Update 数组相关.md

master
Omooo 5 years ago
parent 2a872f0093
commit 52d5e51b2a
  1. 35
      blogs/Algorithm/剑指 Offer/数组相关.md

@ -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;
}
}
```

Loading…
Cancel
Save