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