File tree Expand file tree Collapse file tree 5 files changed +71
-2
lines changed Expand file tree Collapse file tree 5 files changed +71
-2
lines changed Original file line number Diff line number Diff line change @@ -161,6 +161,25 @@ class Solution {
161
161
}
162
162
```
163
163
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
+
164
183
Python:
165
184
``` python
166
185
class Solution :
Original file line number Diff line number Diff line change @@ -199,6 +199,36 @@ class Solution {
199
199
}
200
200
```
201
201
202
+ ``` java
203
+ // DP
204
+ class Solution {
205
+ public int wiggleMaxLength (int [] nums ) {
206
+ // 0 i 作为波峰的最大长度
207
+ // 1 i 作为波谷的最大长度
208
+ int dp[][] = new int [nums. length][2 ];
209
+
210
+ dp[0 ][0 ] = dp[0 ][1 ] = 1 ;
211
+ for (int i = 1 ; i < nums. length; i++ ){
212
+ // i 自己可以成为波峰或者波谷
213
+ dp[i][0 ] = dp[i][1 ] = 1 ;
214
+
215
+ for (int j = 0 ; j < i; j++ ){
216
+ if (nums[j] > nums[i]){
217
+ // i 是波谷
218
+ dp[i][1 ] = Math . max(dp[i][1 ], dp[j][0 ] + 1 );
219
+ }
220
+ if (nums[j] < nums[i]){
221
+ // i 是波峰
222
+ dp[i][0 ] = Math . max(dp[i][0 ], dp[j][1 ] + 1 );
223
+ }
224
+ }
225
+ }
226
+
227
+ return Math . max(dp[nums. length - 1 ][0 ], dp[nums. length - 1 ][1 ]);
228
+ }
229
+ }
230
+ ```
231
+
202
232
Python:
203
233
``` python3
204
234
class Solution :
Original file line number Diff line number Diff line change @@ -186,6 +186,26 @@ class Solution {
186
186
}
187
187
```
188
188
189
+ ``` java
190
+ // 非压缩状态的版本
191
+ class Solution {
192
+ public int fib (int n ) {
193
+ if (n <= 1 ) return n;
194
+
195
+ int [] dp = new int [n + 1 ];
196
+
197
+ dp[0 ] = 0 ;
198
+ dp[1 ] = 1 ;
199
+
200
+ for (int index = 2 ; index <= n; index++ ){
201
+ dp[index] = dp[index - 1 ] + dp[index - 2 ];
202
+ }
203
+
204
+ return dp[n];
205
+ }
206
+ }
207
+ ```
208
+
189
209
Python:
190
210
``` python3
191
211
class Solution :
Original file line number Diff line number Diff line change @@ -54,7 +54,7 @@ traversal(cur->left, vec); // 左
54
54
traversal(cur->right, vec); // 右
55
55
```
56
56
57
- 单层递归的逻辑就是按照中左右的顺序来处理的,这样二叉树的前序遍历,基本就写完了,在看一下完整代码 :
57
+ 单层递归的逻辑就是按照中左右的顺序来处理的,这样二叉树的前序遍历,基本就写完了,再看一下完整代码 :
58
58
59
59
前序遍历:
60
60
Original file line number Diff line number Diff line change 32
32
33
33
## 哈希函数
34
34
35
- 哈希函数,把学生的姓名直接映射为哈希表上的索引,然后就可以通过查询索引下表快速知道这位同学是否在这所学校里了 。
35
+ 哈希函数,把学生的姓名直接映射为哈希表上的索引,然后就可以通过查询索引下标快速知道这位同学是否在这所学校里了 。
36
36
37
37
哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。
38
38
You can’t perform that action at this time.
0 commit comments