diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index b8a9d748..b8b11a57 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -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: