From c8cc5b51207e045f594209332009d985545ca52c Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 24 Oct 2021 09:51:56 -0400 Subject: [PATCH] =?UTF-8?q?Update=200053.=E6=9C=80=E5=A4=A7=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加dp方法 --- problems/0053.最大子序和.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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: