We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7d6790e commit fdb48d9Copy full SHA for fdb48d9
problems/0053.最大子序和(动态规划).md
@@ -95,7 +95,32 @@ public:
95
96
97
Java:
98
+```java
99
+ /**
100
+ * 1.dp[i]代表当前下标对应的最大值
101
+ * 2.递推公式 dp[i] = max (dp[i-1]+nums[i],nums[i]) res = max(res,dp[i])
102
+ * 3.初始化 都为 0
103
+ * 4.遍历方向,从前往后
104
+ * 5.举例推导结果。。。
105
+ *
106
+ * @param nums
107
+ * @return
108
+ */
109
+ public static int maxSubArray(int[] nums) {
110
+ if (nums.length == 0) {
111
+ return 0;
112
+ }
113
114
+ int res = nums[0];
115
+ int[] dp = new int[nums.length];
116
+ dp[0] = nums[0];
117
+ for (int i = 1; i < nums.length; i++) {
118
+ dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
119
+ res = res > dp[i] ? res : dp[i];
120
121
+ return res;
122
123
+```
124
125
Python:
126
0 commit comments