最大子数组 贪心
https://www.lintcode.com/problem/maximum-subarray/description
关键点:因为数据是连续的 即累加值如果小于当前值 则直接切到当前值重新开始为最优 期间res是最优值。
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(int[] nums) {
int res = nums[0], tmp = nums[0];
for(int i = 1; i<nums.length; i++){
if(tmp + nums[i] < nums[i]){
tmp = nums[i];
}else{
tmp += nums[i];
}
res = Math.max(res, tmp);
}
return res;
}
}