auto commit
This commit is contained in:
parent
6eb176d506
commit
f7fd83baf4
@ -804,6 +804,30 @@ public int maxProfit(int[] prices) {
|
||||
}
|
||||
```
|
||||
|
||||
**子数组最大的和**
|
||||
|
||||
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
|
||||
|
||||
```html
|
||||
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
|
||||
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
|
||||
```
|
||||
|
||||
```java
|
||||
public int maxSubArray(int[] nums) {
|
||||
if (nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int preSum = nums[0];
|
||||
int maxSum = preSum;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
preSum = preSum > 0 ? preSum + nums[i] : nums[i];
|
||||
maxSum = Math.max(maxSum, preSum);
|
||||
}
|
||||
return maxSum;
|
||||
}
|
||||
```
|
||||
|
||||
## 二分查找
|
||||
|
||||
**正常实现**
|
||||
@ -2586,30 +2610,6 @@ class NumArray {
|
||||
}
|
||||
```
|
||||
|
||||
**子数组最大的和**
|
||||
|
||||
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
|
||||
|
||||
```html
|
||||
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
|
||||
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
|
||||
```
|
||||
|
||||
```java
|
||||
public int maxSubArray(int[] nums) {
|
||||
if (nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int preSum = nums[0];
|
||||
int maxSum = preSum;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
preSum = preSum > 0 ? preSum + nums[i] : nums[i];
|
||||
maxSum = Math.max(maxSum, preSum);
|
||||
}
|
||||
return maxSum;
|
||||
}
|
||||
```
|
||||
|
||||
**数组中等差递增子区间的个数**
|
||||
|
||||
[413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/)
|
||||
|
Loading…
x
Reference in New Issue
Block a user