Skip to content

Commit

Permalink
添加 0053.最大子序和.md Java版本
Browse files Browse the repository at this point in the history
  • Loading branch information
YiwenXie authored May 12, 2021
1 parent 4180f16 commit a779014
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion problems/0053.最大子序和.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,25 @@ public:
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:

Expand Down

0 comments on commit a779014

Please sign in to comment.