Maximum Sum Subarray

题目:

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.

 

代码:

 1 public class Solution {  2     public int maxSubArray(int[] nums)  3  {  4         int len = nums.length;  5         int SubSum = nums[0];  6         int MaxSum = nums[0];  7         for(int i=1;i<len;i++)  8  {  9             SubSum=Math.max(nums[i],SubSum+nums[i]); 10             MaxSum=Math.max(MaxSum,SubSum); 11             

12  } 13         

14         return MaxSum; 15         

16  } 17 }

 

你可能感兴趣的:(array)