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.
输入:给定数组
输出:找出给定数组所包含的连续的子数组中,元素加和最大的那一组,返回最大和
思路:数组
int max = Integer.MIN_VALUE; int nowValue = 0; for(int i=0;i<nums.length;i++){ if(nowValue < 0){ nowValue = nums[i]; max = Math.max(max, nowValue); }else{ nowValue = nowValue + nums[i]; max = Math.max(max, nowValue); } } return max;