It is a bit interesting to summarize an integer set by continuity.

Source: Internet
Author: User

Some algorithms are boring when we learn them, but if they can solve problems in practical applications, they will become very interesting.

Today, I encountered a requirement in development to summarize an unordered Integer Set Based on the continuity between numbers, as shown in figure

3, 1, 2, 4, 9, 8, 10, 6

After the eight numbers are summarized based on continuity, a summary string in the following form should be generated. Note that the continuous number is summarized.

1-4, 6, 8-9, 10

We need to provide a public method to solve this problem:

string GetStrFromIntList(List<Int> listNumbers);
Solution 1 least data accumulation method

So how can we achieve this? First, I thought of a method:

1. loop this set to find the smallest number N.

2. Find the number M greater than N 1, and then find the number X greater than M 1, and so on until the last continuous number of 1 is found.

3. Remove the number that has been found and save it to another set. continue to the next round.

The initial statement is as follows:

Static string GetStrByIntNumbers (List <int> listNumbers) {string strResult = string. empty; // used as the output result List <int> listTemp = new List <int> (); int temp = 0; while (listNumbers. count! = 0) {temp = listNumbers. min (); // obtain the minimum number of listTemp. add (temp); listNumbers. remove (temp); var a = listNumbers. where (m => m-1 = temp ). firstOrDefault (); // find the continuous number greater than 1 if (! = 0) // if there is a number greater than 1, it is added to the continuous number set {listTemp. add (a);} else // if there is no number greater than 1, start searching for the new continuous number segment {var min = listTemp. min (); var max = listTemp. max (); if (min! = Max) {strResult + = min + "-" + max;} else {strResult + = min;} listTemp. clear (); strResult + = "," ;}} strResult = strResult. substring (0, strResult. length-1); return strResult;} now use the actual number to test:
Result:
However, this method is complicated to write. Is there a better way? Do you have one?
Method 2 Bubble sorting and Comparison

In this method, if the parameter is not List <int>, but int [], you need to use Bubble sorting. Here we provide a bubble algorithm.

List <int> listTest = new List <int> () {1, 3, 5, 24, 7, 6, 8, 9, 10, 14}; int temp = 0; for (int I = 0; I <listTest. count; I ++) {for (int j = I + 1; j <listTest. count; j ++) {if (listTest [I]> listTest [j]) {temp = listTest [I]; listTest [I] = listTest [j]; listTest [j] = temp ;}}for (int I = 0; I <listTest. count; I ++) {Console. writeLine ("Row? After "°? Which of the following statements are 2 {0? Oy is a: {1} ", I, listTest [I]);} Of course, we use generic, you can directly sort the size of a set using the generic method:
 
ListTest = listTest. OrderBy (m => m). ToList (); after sorting, we will perform the following operations:
1. Read the element from the first vertex to another set A and delete the element from the set listTest.
2. When listTest is cyclically executed, you must first determine whether the value is equal to 1 greater than the element 1 in set A. If not, add it to set.
List <int> listTest = new List <int> () {1, 3, 5, 24, 7, 6, 8, 9, 10, 14}; int temp = 0; for (int I = 0; I <listTest. count; I ++) {for (int j = I + 1; j <listTest. count; j ++) {if (listTest [I]> listTest [j]) {temp = listTest [I]; listTest [I] = listTest [j]; listTest [j] = temp ;}} string strTemp = string. empty; List <int> listResult = new List <int> (); foreach (var item in listTest) {if (listResult. count = 0) {listResult. add (item);} else {if (listResult. max () + 1 = item) {listResult. add (item);} else {var min = listResult. min (); var max = listResult. max (); strTemp + = (min = max )? (Min + ","): (min + "-" + max + ","); listResult. clear (); listResult. add (item) ;}} Console. writeLine (strTemp); Console. read (); I believe there is a better way. Think about it again...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.