获取数组中的连续和最大的子数组

 

   class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { -2, 1, 1, 1, -2, -3, 4, -1, 2, 1, -5, 10 };

            Dictionary<int, MaxArr> dic = new Dictionary<int, MaxArr>();
            List<int> list = new List<int>();
            int maxValue = 0;
            MaxArr maxArr = new MaxArr();
            for (int i = 0; i < arr.Length; i++)
            {
                //第一次,初始化。
                if (i == 0)
                {
                    maxValue = arr[0];
                    maxArr.OldTotalCount = maxValue;
                    maxArr.TotalCount = maxValue;
                    maxArr.list.Add(maxValue);
                    maxArr.maxValueListLength = 1;
                }
                else
                {
                    //获取最大值
                    if (maxValue < arr[i])
                    {
                        maxValue = arr[i];
                    }
//探查可能最大值 maxArr.list.Add(arr[i]); maxArr.TotalCount += arr[i]; //如果小于最大值,且小于0,那么可以保存已经加入集合的数组。重新开始了。 if (maxArr.TotalCount < maxValue && maxArr.TotalCount <= 0) { //如果结束时,计算的最大值小于曾经计算出的最大值,进行回退。 if (maxArr.TotalCount < maxArr.OldTotalCount) { while (maxArr.maxValueListLength < maxArr.list.Count) { maxArr.list.RemoveAt(maxArr.list.Count - 1); } } //保存当前数组 if (dic.Count == 0) { dic.Add(0, maxArr); } else { dic.Add(dic.Count, maxArr); } //新的数组保存对象 maxArr = new MaxArr(); //如果当前值大于0则可以作为开始起点 if (maxValue > 0 && arr[i] > 0) { maxArr.list.Add(arr[i]); maxArr.TotalCount = arr[i]; maxArr.OldTotalCount = arr[i]; maxArr.maxValueListLength = maxArr.list.Count; } else if (maxValue <= 0 && arr[i] >= maxValue) { maxArr.list.Add(arr[i]); maxArr.TotalCount = arr[i]; maxArr.OldTotalCount = arr[i]; maxArr.maxValueListLength = maxArr.list.Count; } } else { if (maxArr.TotalCount > maxArr.OldTotalCount) { maxArr.OldTotalCount = maxArr.TotalCount; maxArr.maxValueListLength = maxArr.list.Count; } if (i == arr.Length - 1) { if (maxArr.TotalCount < maxArr.OldTotalCount) { while (maxArr.maxValueListLength > maxArr.list.Count) { maxArr.list.RemoveAt(maxArr.list.Count - 1); } } dic.Add(dic.Count , maxArr); } } } } Console.WriteLine(maxArr.TotalCount); Console.ReadKey(); } public class MaxArr { public int TotalCount { get; set; } public int OldTotalCount { get; set; } public int maxValueListLength { get; set; } public List<int> list = new List<int>(); } }

 分析思路

源数组:-2——1——-3——4——-1——2——1——-5——4

开始循环

最大值-2
list加入数字,-2,和-2,长度1
下一次
最大值1
list加入数字,-21,和-1,长度2,小于最大值1,且小于0。不再继续探索,存储当前数组,1,和1,长度1,清空list
下一次
最大值1
list为空,当前item值-3小于最大值,且小于0,放弃本次记录,不作为新数组开始。
下一次
最大值4
值大于0,可以作为开始位置,数组4,当前长度1,当前和4,最大和4,最大和时长度1
下一次
最大值4
加入数字,数组4,-1,当前长度2,当前和3,
检查后,不需要更新最大和值,不需要更新最大和时长度
下一次
最大值4
加入数字,数组4,-12,当前长度3,当前和5,检查后,需要更新最大和值变为5,最大和时长度3
下一次
最大值4
加入数字,数组4,-121
……
下一次
最大值4
加入数字,数组4,-121,-5,当前长度5,当前和1,检查后,和大于0不需要结束,不需要更新最大和值,不需要更新最大和时长度
下一次
最大值4
加入数字,数组4,-121,-54,当前长度6,当前和5,经检查,源数组最后一位,当前和小于最大和,需要根据最大和时list长度,移出后来加入的数字。检查边界进行移除,移出完毕。保存当前list。
for循环结束

遍历Dictionary给出最终结果。

 

posted on 2018-04-20 14:28  荆棘人  阅读(239)  评论(0编辑  收藏  举报

导航