求和最大的数字子串

由于博客内容为空,无法提供包含关键信息的摘要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    /*
     * 
     * Problem 3:  
     *  Complete a function to find the sub array of an integer array with
     * largest sum. For example, for the array [1, -2, 3, 10, -4, 7, 2, -5],
     * you should find 3 + 10 + (-4) + 7 + 2 = 18. It is best to write the
     * code in C#.
     * 
     */
    class Program
    {
        static void Main(string[] args)
        {
            List<int> test1 = new List<int>()
            {
                1, -2, 3, 10, -4, 7, 2, -5
            };


            List<int> test2 = new List<int>()
            {
                1, 5 , 11, -21, 7 , 3 ,6, 9, -15
            };

            List<int> result1 = MaxSumArray(test1);
            List<int> result2 = MaxSumArray(test2);

            System.Console.WriteLine(string.Join(" ", result1.ToArray()));
            System.Console.WriteLine(string.Join(" ", result2.ToArray()));

            System.Console.ReadLine();

        }
        /*
         * When we add a positive number, the sum will increase;
         * When we add a negative number, the sum will decrease.
         * If the sum currently obtained is a negative number,
         * then this sum should be discarded and reset in the subsequent accumulation.
         */
        static List<int> MaxSumArray(List<int> input)
        {
            List<int> ret = new List<int>();
            /// Find out the continuous sub array of input that has maximum summation.
            /// Required to be O(n)
            ///
            if (input == null)
            {
                return ret;
            }

            int curSum = 0, maxSum = 0;
            /// Initializing the maximum and end index
            int curSumIndexStart = 0;
            int maxSumIndexStart = 0, maxSumIndexEnd = 0;
            int i = 0, len = input.Count;
            maxSum = input[0];

            for (i = 0; i < len; i++)
            {
                // the maximum value of the "index_start"
                curSum += input[i];

                if (curSum > maxSum)
                {
                    // The current sum is greater than the largest.
                    // Update the largest.
                    // Record the index.
                    maxSum = curSum;

                    maxSumIndexStart = curSumIndexStart;
                    maxSumIndexEnd = i;
                }

                if (curSum < 0)
                {
                    // The current sum is less than 0,
                    // Set curSum to 0.
                    // Set index_start
                    curSum = 0;
                    curSumIndexStart = i + 1;
                }
            }

            ret = input.GetRange(maxSumIndexStart, maxSumIndexEnd + 1 - maxSumIndexStart);

            return ret;

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值