mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
@ -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:
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
@ -199,6 +199,36 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
// DP
|
||||||
|
class Solution {
|
||||||
|
public int wiggleMaxLength(int[] nums) {
|
||||||
|
// 0 i 作为波峰的最大长度
|
||||||
|
// 1 i 作为波谷的最大长度
|
||||||
|
int dp[][] = new int[nums.length][2];
|
||||||
|
|
||||||
|
dp[0][0] = dp[0][1] = 1;
|
||||||
|
for (int i = 1; i < nums.length; i++){
|
||||||
|
//i 自己可以成为波峰或者波谷
|
||||||
|
dp[i][0] = dp[i][1] = 1;
|
||||||
|
|
||||||
|
for (int j = 0; j < i; j++){
|
||||||
|
if (nums[j] > nums[i]){
|
||||||
|
// i 是波谷
|
||||||
|
dp[i][1] = Math.max(dp[i][1], dp[j][0] + 1);
|
||||||
|
}
|
||||||
|
if (nums[j] < nums[i]){
|
||||||
|
// i 是波峰
|
||||||
|
dp[i][0] = Math.max(dp[i][0], dp[j][1] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.max(dp[nums.length - 1][0], dp[nums.length - 1][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
|
@ -186,6 +186,26 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
//非压缩状态的版本
|
||||||
|
class Solution {
|
||||||
|
public int fib(int n) {
|
||||||
|
if (n <= 1) return n;
|
||||||
|
|
||||||
|
int[] dp = new int[n + 1];
|
||||||
|
|
||||||
|
dp[0] = 0;
|
||||||
|
dp[1] = 1;
|
||||||
|
|
||||||
|
for (int index = 2; index <= n; index++){
|
||||||
|
dp[index] = dp[index - 1] + dp[index - 2];
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
|
@ -54,7 +54,7 @@ traversal(cur->left, vec); // 左
|
|||||||
traversal(cur->right, vec); // 右
|
traversal(cur->right, vec); // 右
|
||||||
```
|
```
|
||||||
|
|
||||||
单层递归的逻辑就是按照中左右的顺序来处理的,这样二叉树的前序遍历,基本就写完了,在看一下完整代码:
|
单层递归的逻辑就是按照中左右的顺序来处理的,这样二叉树的前序遍历,基本就写完了,再看一下完整代码:
|
||||||
|
|
||||||
前序遍历:
|
前序遍历:
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
## 哈希函数
|
## 哈希函数
|
||||||
|
|
||||||
哈希函数,把学生的姓名直接映射为哈希表上的索引,然后就可以通过查询索引下表快速知道这位同学是否在这所学校里了。
|
哈希函数,把学生的姓名直接映射为哈希表上的索引,然后就可以通过查询索引下标快速知道这位同学是否在这所学校里了。
|
||||||
|
|
||||||
哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。
|
哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user