按连续性汇总整形集合,有点儿意思。

有些算法我们在学习时会觉得枯燥无味,但是如果在实际应用中能够解决问题,就会变得很有意思。

今天在开发中遇到一个需求,将一个无序的整形集合,按数字之间的连续性进行汇总,如

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

这八个数按照连续性汇总后,应该生成如下形式的汇总字符串,注意是对连续的数进行了汇总

1-4,6,8-9,10

我们需要提供一个公共方法来处理这个问题:

string GetStrFromIntList(List<Int> listNumbers);

方案一 最小数累积法

那么怎么实现这个需求呢,我首先想到了一个方法:

1,循环这个集合,找出最小的数N

2,找出比N大1的数M,再按此逻辑找到比M大1的数X,以此类推,直到找出最后一个连续大1的数

3,将已找出的数剔除掉并存到另外一个集合,再继续下一轮

最开始的写法是这样的:

static string GetStrByIntNumbers(List<int> listNumbers)
        {
            string strResult = string.Empty;//用作输出结果
            List<int> listTemp = new List<int>();
            int temp = 0;
            while (listNumbers.Count != 0)
            {
                temp = listNumbers.Min();//取得最小数
                listTemp.Add(temp);
                listNumbers.Remove(temp);
                var a = listNumbers.Where(m => m - 1 == temp).FirstOrDefault();//找出大于1的连续数
                if (a != 0)//如果有大于1的数则增加进连续数的集合
                {
                    listTemp.Add(a);
                }
                else //如果没有大于1的数则开始新的连续数段寻找
                {
                    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;
        }
现在来使用实际的数来测试:
image
结果:
image
但是这个方法的写法实在有些繁琐,有没有更好的办法呢?您有吗?

第二种方法 冒泡排序 再比较

这种方法,如果参数不是List<int>而是,int[]的话就需要使用到冒泡排序了,这里给出一个冒泡的算法

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("排?序¨°后¨?第ì¨2{0}个?数oy为a:{1}", i, listTest[i]);
            }
当然,我们使用的是泛型,可以直接用泛型中的方法对集合进行大小排序:
 
listTest = listTest.OrderBy(m => m).ToList();
排序完成后呢,我们将进行如下操作
1,从第一位开始,读入到另外的集合A中,并在集合listTest中删除此元素
2,循环listTest时,需先判断是不是刚好比集合A中元素大1,是则需加到集合A,不是的话另外加
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();
我相信有更好的办法的,再想想喽。。。
posted @ 2011-04-21 23:28  翁玉礼  阅读(799)  评论(0编辑  收藏  举报