思路: sum = 0,max = num[0], 依次遍历一个一个往前加,sum = sum + num[i], 如果sum[i] 小,则替换sum 如果·sum > max, 则max = sum;
intmaxSubArray(int* nums,int numsSize){int max = nums[0];int sum =0;for(int i =0; i < numsSize; i++){
sum = sum + nums[i];if(sum < nums[i]){
sum = nums[i];}if(sum > max){
max = sum;}}return max;}
固定窗长 要求固定k长度数组的和 先求固定长度的和,然后sum里面加后面的,丢弃前面的
intgetResult(int*arr,int arr_size,int k){int window_sum =0;for(int i =0; i < k; i++){
window_sum += arr[i];}int ans = window_sum ;
for(int j =1; j <= arr_size - k; j++){
window_sum -= arr[i-1];
window_sum += arr[i+k-1];
ans =MAX(ans, window_sum );}return ans;}