From 90ed55b5da370129f9d50691c7d1985642c2a818 Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 24 Oct 2021 08:01:53 -0400 Subject: [PATCH 1/5] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字 --- problems/二叉树的递归遍历.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index eee150b7..209cbcb7 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -54,7 +54,7 @@ traversal(cur->left, vec); // 左 traversal(cur->right, vec); // 右 ``` -单层递归的逻辑就是按照中左右的顺序来处理的,这样二叉树的前序遍历,基本就写完了,在看一下完整代码: +单层递归的逻辑就是按照中左右的顺序来处理的,这样二叉树的前序遍历,基本就写完了,再看一下完整代码: 前序遍历: From c8cc5b51207e045f594209332009d985545ca52c Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 24 Oct 2021 09:51:56 -0400 Subject: [PATCH 2/5] =?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: From f9abccf8892f4d7054806a97a84110846e3d047f Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 24 Oct 2021 09:53:42 -0400 Subject: [PATCH 3/5] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加dp方法 --- problems/0376.摆动序列.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 5587a8c7..e58a26ff 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -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: ```python3 class Solution: From 61838527a072acd7cf7fb4e3fce8de0f4ff91fce Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 24 Oct 2021 10:07:34 -0400 Subject: [PATCH 4/5] =?UTF-8?q?Update=200509.=E6=96=90=E6=B3=A2=E9=82=A3?= =?UTF-8?q?=E5=A5=91=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加java非压缩状态版本,易于理解 --- problems/0509.斐波那契数.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 4102dd64..cb54a0f9 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -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: ```python3 class Solution: From 09e82fa59602210006e8fbc9977119bcd3b3030c Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 24 Oct 2021 10:28:31 -0400 Subject: [PATCH 5/5] =?UTF-8?q?Update=20=E5=93=88=E5=B8=8C=E8=A1=A8?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix typo --- problems/哈希表理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/哈希表理论基础.md b/problems/哈希表理论基础.md index aeb11b2e..05fb2a32 100644 --- a/problems/哈希表理论基础.md +++ b/problems/哈希表理论基础.md @@ -32,7 +32,7 @@ ## 哈希函数 -哈希函数,把学生的姓名直接映射为哈希表上的索引,然后就可以通过查询索引下表快速知道这位同学是否在这所学校里了。 +哈希函数,把学生的姓名直接映射为哈希表上的索引,然后就可以通过查询索引下标快速知道这位同学是否在这所学校里了。 哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。