|
|
@ -124,3 +124,38 @@ class Solution { |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[42. 连续子数组的最大和](https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```java |
|
|
|
|
|
|
|
class Solution { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int maxSubArray(int[] nums) { |
|
|
|
|
|
|
|
int max = Integer.MIN_VALUE; |
|
|
|
|
|
|
|
int sum = 0; |
|
|
|
|
|
|
|
for (int num : nums) { |
|
|
|
|
|
|
|
if (sum <= 0) { |
|
|
|
|
|
|
|
sum = num; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
sum += num; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
max = Math.max(max, sum); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return max; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```java |
|
|
|
|
|
|
|
class Solution { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int maxSubArray(int[] nums) { |
|
|
|
|
|
|
|
int res = nums[0]; |
|
|
|
|
|
|
|
for (int i = 1; i < nums.length; i++) { |
|
|
|
|
|
|
|
nums[i] += Math.max(nums[i - 1], 0); |
|
|
|
|
|
|
|
res = Math.max(res, nums[i]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return res; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|