Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c8cc5b5

Browse files
authored
Update 0053.最大子序和.md
增加dp方法
1 parent 90ed55b commit c8cc5b5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

problems/0053.最大子序和.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ class Solution {
161161
}
162162
```
163163

164+
```java
165+
// DP 方法
166+
class Solution {
167+
public int maxSubArray(int[] nums) {
168+
int ans = Integer.MIN_VALUE;
169+
int[] dp = new int[nums.length];
170+
dp[0] = nums[0];
171+
ans = dp[0];
172+
173+
for (int i = 1; i < nums.length; i++){
174+
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
175+
ans = Math.max(dp[i], ans);
176+
}
177+
178+
return ans;
179+
}
180+
}
181+
```
182+
164183
Python:
165184
```python
166185
class Solution:

0 commit comments

Comments
 (0)