简介
暴力只要变量两遍, 挺好的, 不过更好的应该是动态规划. 应该是最简单的动态规划了吧
code
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max_num = nums[0];
for(int i=0; i<nums.size(); i++){
int sum = nums[i];
max_num = max(max_num, sum);
for(int j=i+1; j<nums.size(); j++){
sum += nums[j];
max_num = max(max_num, sum);
}
}
return max_num;
}
};
class Solution {
public int maxSubArray(int[] nums) {
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(nums[0]);
for(int i=1; i<nums.length; i++){
a.add(Math.max((Integer)a.get(i-1) + nums[i], nums[i]));
}
int max = -100000000;
for(Integer i: a){
max = Math.max(i, max);
}
return max;
}
}