mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 0053.最大子序和.md Java版本
This commit is contained in:
@ -139,7 +139,25 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int maxSubArray(int[] nums) {
|
||||||
|
if (nums.length == 1){
|
||||||
|
return nums[0];
|
||||||
|
}
|
||||||
|
int sum = Integer.MIN_VALUE;
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < nums.length; i++){
|
||||||
|
count += nums[i];
|
||||||
|
sum = Math.max(sum, count); // 取区间累计的最大值(相当于不断确定最大子序终止位置)
|
||||||
|
if (count <= 0){
|
||||||
|
count = 0; // 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user