Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5e7a5fc

Browse files
Merge pull request youngyangyang04#872 from hailincai/master
修改了错别字和增加dp方法
2 parents 1e2695c + 25eee5a commit 5e7a5fc

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

problems/0053.最大子序和.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ class Solution {
161161
}
162162
```
163163

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+
164183
Python:
165184
```python
166185
class Solution:

problems/0376.摆动序列.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,36 @@ class Solution {
199199
}
200200
```
201201

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+
202232
Python:
203233
```python3
204234
class Solution:

problems/0509.斐波那契数.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ class Solution {
186186
}
187187
```
188188

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+
189209
Python:
190210
```python3
191211
class Solution:

problems/二叉树的递归遍历.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ traversal(cur->left, vec); // 左
5454
traversal(cur->right, vec); // 右
5555
```
5656

57-
单层递归的逻辑就是按照中左右的顺序来处理的,这样二叉树的前序遍历,基本就写完了,在看一下完整代码
57+
单层递归的逻辑就是按照中左右的顺序来处理的,这样二叉树的前序遍历,基本就写完了,再看一下完整代码
5858

5959
前序遍历:
6060

problems/哈希表理论基础.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
## 哈希函数
3434

35-
哈希函数,把学生的姓名直接映射为哈希表上的索引,然后就可以通过查询索引下表快速知道这位同学是否在这所学校里了
35+
哈希函数,把学生的姓名直接映射为哈希表上的索引,然后就可以通过查询索引下标快速知道这位同学是否在这所学校里了
3636

3737
哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。
3838

0 commit comments

Comments
 (0)