mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0053.最大子序和.md
增加dp方法
This commit is contained in:
@ -161,6 +161,25 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
// DP 方法
|
||||
class Solution {
|
||||
public int maxSubArray(int[] nums) {
|
||||
int ans = Integer.MIN_VALUE;
|
||||
int[] dp = new int[nums.length];
|
||||
dp[0] = nums[0];
|
||||
ans = dp[0];
|
||||
|
||||
for (int i = 1; i < nums.length; i++){
|
||||
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
|
||||
ans = Math.max(dp[i], ans);
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
class Solution:
|
||||
|
Reference in New Issue
Block a user