LintCode 41---最大子数组

public class Solution {
    /**
     * @param nums: A list of integers
     * @return: A integer indicate the sum of max subarray
     */
    public int maxSubArray(int[] nums) {
      int n = nums.length;
      int ans = -1000000;
      int sum = 0;
      for(int i=0; i<n; i++)
        {
            sum += nums[i];
            if(sum > ans)
            {
                ans = sum;
            }
            if(sum < 0)
            {
                sum = 0;   //子串和为负数,丢掉
            }
        }
     return ans;
    }
}

 

上一篇:LintCode 44---最小子数组


下一篇:C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成