From b6b73f20a541cb20ec12373b25cacd1fe495745b Mon Sep 17 00:00:00 2001
From: callmePicacho <38685653+callmePicacho@users.noreply.github.com>
Date: Thu, 8 Jul 2021 11:24:14 +0800
Subject: [PATCH 01/28] =?UTF-8?q?Update=200047.=E5=85=A8=E6=8E=92=E5=88=97?=
=?UTF-8?q?II.md=20Go=20=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0047.全排列II.md | 38 ++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md
index 079ca834..b6c12919 100644
--- a/problems/0047.全排列II.md
+++ b/problems/0047.全排列II.md
@@ -5,6 +5,7 @@
欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+ # 排列问题(二) ## 47.全排列 II @@ -222,6 +223,43 @@ class Solution: return res ``` +Go: + +```go +var res [][]int +func permute(nums []int) [][]int { + res = [][]int{} + sort.Ints(nums) + dfs(nums, make([]int, 0), make([]bool, len(nums))) + return res +} + +func dfs(nums, path []int, used []bool) { + if len(path) == len(nums) { + res = append(res, append([]int{}, path...)) + return + } + + m := make(map[int]bool) + for i := 0; i < len(nums); i++ { + // used 从剩余 nums 中选 + if used[i] { + continue + } + // m 集合间去重 + if _, ok := m[nums[i]]; ok { + continue + } + m[nums[i]] = true + path = append(path, nums[i]) + used[i] = true + dfs(nums, path, used) + used[i] = false + path = path[:len(path)-1] + } +} +``` + Javascript: ```javascript From cd37799b72126f0365358375133e460bbb1411b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=A4=E9=9C=B2?= <080301087@163.com> Date: Thu, 8 Jul 2021 16:04:46 +0800 Subject: [PATCH 02/28] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20-=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86python3=E7=89=88=E6=9C=AC=E7=9A=84=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E9=80=92=E5=BD=92=E6=B3=95=E5=92=8C=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index b93d8cd5..4652fb86 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -343,7 +343,7 @@ Python: # self.val = val # self.left = left # self.right = right -//递归法 +# 递归法 class Solution: def isValidBST(self, root: TreeNode) -> bool: res = [] //把二叉搜索树按中序遍历写成list @@ -355,6 +355,35 @@ class Solution: return res buildalist(root) return res == sorted(res) and len(set(res)) == len(res) //检查list里的数有没有重复元素,以及是否按从小到大排列 + +# 简单递归法 +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + def isBST(root, min_val, max_val): + if not root: return True + if root.val >= max_val or root.val <= min_val: + return False + return isBST(root.left, min_val, root.val) and isBST(root.right, root.val, max_val) + return isBST(root, float("-inf"), float("inf")) + +# 迭代-中序遍历 +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + stack = [] + cur = root + pre = None + while cur or stack: + if cur: # 指针来访问节点,访问到最底层 + stack.append(cur) + cur = cur.left + else: # 逐一处理节点 + cur = stack.pop() + if pre and cur.val <= pre.val: # 比较当前节点和前节点的值的大小 + return False + pre = cur + cur = cur.right + return True + ``` Go: ```Go From 3ab914332ebfe60020061d2bc4bd9102687f4841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=A4=E9=9C=B2?= <080301087@163.com> Date: Thu, 8 Jul 2021 17:02:29 +0800 Subject: [PATCH 03/28] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md=20-=20=E5=A2=9E=E5=8A=A0=E4=BA=86python3=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E8=BF=AD=E4=BB=A3=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 47b2b434..bf646443 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -222,6 +222,26 @@ class Solution: for i in range(len(res)-1): // 统计有序数组的最小差值 r = min(abs(res[i]-res[i+1]),r) return r + +# 迭代法-中序遍历 +class Solution: + def getMinimumDifference(self, root: TreeNode) -> int: + stack = [] + cur = root + pre = None + result = float('inf') + while cur or stack: + if cur: # 指针来访问节点,访问到最底层 + stack.append(cur) + cur = cur.left + else: # 逐一处理节点 + cur = stack.pop() + if pre: # 当前节点和前节点的值的差值 + result = min(result, cur.val - pre.val) + pre = cur + cur = cur.right + return result + ``` Go: > 中序遍历,然后计算最小差值 From f5dd57cec562f48b89066db36d2813d6aac968d4 Mon Sep 17 00:00:00 2001 From: hk27xing <244798299@qq.com> Date: Thu, 8 Jul 2021 17:25:57 +0800 Subject: [PATCH 04/28] =?UTF-8?q?=E6=94=B9=E8=BF=9B123,=20188=E7=9A=84Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0123.买卖股票的最佳时机III.md | 60 +++++++++++------- .../0188.买卖股票的最佳时机IV.md | 63 +++++++++++-------- 2 files changed, 75 insertions(+), 48 deletions(-) diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index f6d2efb0..ad91c1aa 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -193,35 +193,49 @@ dp[1] = max(dp[1], dp[0] - prices[i]); 如果dp[1]取dp[1],即保持买入股 Java: ```java -class Solution { // 动态规划 +// 版本一 +class Solution { public int maxProfit(int[] prices) { - // 可交易次数 - int k = 2; + int len = prices.length; + // 边界判断, 题目中 length >= 1, 所以可省去 + if (prices.length == 0) return 0; - // [天数][交易次数][是否持有股票] - int[][][] dp = new int[prices.length][k + 1][2]; + /* + * 定义 5 种状态: + * 0: 没有操作, 1: 第一次买入, 2: 第一次卖出, 3: 第二次买入, 4: 第二次卖出 + */ + int[][] dp = new int[len][5]; + dp[0][1] = -prices[0]; + // 初始化第二次买入的状态是确保 最后结果是最多两次买卖的最大利润 + dp[0][3] = -prices[0]; - // badcase - dp[0][0][0] = 0; - dp[0][0][1] = Integer.MIN_VALUE; - dp[0][1][0] = 0; - dp[0][1][1] = -prices[0]; - dp[0][2][0] = 0; - dp[0][2][1] = Integer.MIN_VALUE; - - for (int i = 1; i < prices.length; i++) { - for (int j = 2; j >= 1; j--) { - // dp公式 - dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]); - dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]); - } + for (int i = 1; i < len; i++) { + dp[i][1] = Math.max(dp[i - 1][1], -prices[i]); + dp[i][2] = Math.max(dp[i - 1][2], dp[i][1] + prices[i]); + dp[i][3] = Math.max(dp[i - 1][3], dp[i][2] - prices[i]); + dp[i][4] = Math.max(dp[i - 1][4], dp[i][3] + prices[i]); } - int res = 0; - for (int i = 1; i < 3; i++) { - res = Math.max(res, dp[prices.length - 1][i][0]); + return dp[len - 1][4]; + } +} + +// 版本二: 空间优化 +class Solution { + public int maxProfit(int[] prices) { + int len = prices.length; + int[] dp = new int[5]; + dp[1] = -prices[0]; + dp[3] = -prices[0]; + + for (int i = 1; i < len; i++) { + dp[1] = Math.max(dp[1], dp[0] - prices[i]); + dp[2] = Math.max(dp[2], dp[1] + prices[i]); + dp[3] = Math.max(dp[3], dp[2] - prices[i]); + dp[4] = Math.max(dp[4], dp[3] + prices[i]); } - return res; + + return dp[4]; } } ``` diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 937aee3a..39950fe0 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -170,41 +170,54 @@ public: Java: ```java -class Solution { //动态规划 +// 版本一: 三维 dp数组 +class Solution { public int maxProfit(int k, int[] prices) { - if (prices == null || prices.length < 2 || k == 0) { - return 0; + if (prices.length == 0) return 0; + + // [天数][交易次数][是否持有股票] + int len = prices.length; + int[][][] dp = new int[len][k + 1][2]; + + // dp数组初始化 + // 初始化所有的交易次数是为确保 最后结果是最多 k 次买卖的最大利润 + for (int i = 0; i <= k; i++) { + dp[0][i][1] = -prices[0]; } - // [天数][交易次数][是否持有股票] - int[][][] dp = new int[prices.length][k + 1][2]; - - // bad case - dp[0][0][0] = 0; - dp[0][0][1] = Integer.MIN_VALUE; - dp[0][1][0] = 0; - dp[0][1][1] = -prices[0]; - // dp[0][j][0] 都均为0 - // dp[0][j][1] 异常值都取Integer.MIN_VALUE; - for (int i = 2; i < k + 1; i++) { - dp[0][i][0] = 0; - dp[0][i][1] = Integer.MIN_VALUE; - } - - for (int i = 1; i < prices.length; i++) { - for (int j = k; j >= 1; j--) { - // dp公式 + for (int i = 1; i < len; i++) { + for (int j = 1; j <= k; j++) { + // dp方程, 0表示不持有/卖出, 1表示持有/买入 dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]); dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]); } } + return dp[len - 1][k][0]; + } +} - int res = 0; - for (int i = 1; i < k + 1; i++) { - res = Math.max(res, dp[prices.length - 1][i][0]); +// 版本二: 空间优化 +class Solution { + public int maxProfit(int k, int[] prices) { + if (prices.length == 0) return 0; + + // [天数][股票状态] + // 股票状态: 奇数表示第 k 次交易持有/买入, 偶数表示第 k 次交易不持有/卖出, 0 表示没有操作 + int len = prices.length; + int[][] dp = new int[len][k*2 + 1]; + + // dp数组的初始化, 与版本一同理 + for (int i = 1; i < k*2; i += 2) { + dp[0][i] = -prices[0]; } - return res; + for (int i = 1; i < len; i++) { + for (int j = 0; j < k*2 - 1; j += 2) { + dp[i][j + 1] = Math.max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]); + dp[i][j + 2] = Math.max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]); + } + } + return dp[len - 1][k*2]; } } ``` From 64989ffe040c4ac06195a6a40655aac435e0d956 Mon Sep 17 00:00:00 2001 From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com> Date: Fri, 9 Jul 2021 07:51:36 +0800 Subject: [PATCH 05/28] =?UTF-8?q?Update=201143.=E6=9C=80=E9=95=BF=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补充1143题目链接 --- problems/1143.最长公共子序列.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index 2ddab584..8e245c20 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -8,6 +8,8 @@ ## 1143.最长公共子序列 +题目链接: https://leetcode-cn.com/problems/longest-common-subsequence/ + 给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。 一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。 From 20fd272c6380a9edcbf831820b7ea0ddbb4039fc Mon Sep 17 00:00:00 2001 From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com> Date: Fri, 9 Jul 2021 08:51:43 +0800 Subject: [PATCH 06/28] =?UTF-8?q?Update=201035.=E4=B8=8D=E7=9B=B8=E4=BA=A4?= =?UTF-8?q?=E7=9A=84=E7=BA=BF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加题目链接 --- problems/1035.不相交的线.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md index c7481306..cb74ef75 100644 --- a/problems/1035.不相交的线.md +++ b/problems/1035.不相交的线.md @@ -8,6 +8,8 @@ ## 1035.不相交的线 +题目链接: https://leetcode-cn.com/problems/uncrossed-lines/ + 我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数。 现在,我们可以绘制一些连接两个数字 A[i] 和 B[j] 的直线,只要 A[i] == B[j],且我们绘制的直线不与任何其他连线(非水平线)相交。 From 6eaa76bf9f6b08d6f9c167f8f165c7c74206d293 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Thu, 8 Jul 2021 21:30:54 -0700 Subject: [PATCH 07/28] add js solution for maxProfit II --- ...票的最佳时机II(动态规划).md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index 1215025e..8ed70063 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -201,6 +201,29 @@ class Solution: Go: +Javascript: +```javascript +const maxProfit = (prices) => { + let dp = Array.from(Array(prices.length), () => Array(2).fill(0)); + // dp[i][0] 表示第i天持有股票所得现金。 + // dp[i][1] 表示第i天不持有股票所得最多现金 + dp[0][0] = 0 - prices[0]; + dp[0][1] = 0; + for(let i = 1; i < prices.length; i++) { + // 如果第i天持有股票即dp[i][0], 那么可以由两个状态推出来 + // 第i-1天就持有股票,那么就保持现状,所得现金就是昨天持有股票的所得现金 即:dp[i - 1][0] + // 第i天买入股票,所得现金就是昨天不持有股票的所得现金减去 今天的股票价格 即:dp[i - 1][1] - prices[i] + dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] - prices[i]); + + // 在来看看如果第i天不持有股票即dp[i][1]的情况, 依然可以由两个状态推出来 + // 第i-1天就不持有股票,那么就保持现状,所得现金就是昨天不持有股票的所得现金 即:dp[i - 1][1] + // 第i天卖出股票,所得现金就是按照今天股票佳价格卖出后所得现金即:prices[i] + dp[i - 1][0] + dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]); + } + + return dp[prices.length -1][0]; +}; +``` From 77e75035d321ff39dc0cf30b2a1e02c6f87edfbb Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Fri, 9 Jul 2021 10:39:19 +0200 Subject: [PATCH 08/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200188=20=E4=B9=B0?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?IV=20python3=E7=89=88=E6=9C=AC=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 0188 买股票的最佳时机IV python3版本二 --- .../0188.买卖股票的最佳时机IV.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 431c292b..daaed8a9 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -211,7 +211,7 @@ class Solution { //动态规划 Python: - +版本一 ```python class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: @@ -226,7 +226,22 @@ class Solution: dp[i][j+2] = max(dp[i-1][j+2], dp[i-1][j+1] + prices[i]) return dp[-1][2*k] ``` - +版本二 +```python3 +class Solution: + def maxProfit(self, k: int, prices: List[int]) -> int: + if len(prices) == 0: return 0 + dp = [0] * (2*k + 1) + for i in range(1,2*k,2): + dp[i] = -prices[0] + for i in range(1,len(prices)): + for j in range(1,2*k + 1): + if j % 2: + dp[j] = max(dp[j],dp[j-1]-prices[i]) + else: + dp[j] = max(dp[j],dp[j-1]+prices[i]) + return dp[2*k] +``` Go: From 3cff2bbe8334b5a23448674d3f2643650f28146d Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 20:10:06 -0700 Subject: [PATCH 09/28] add js solution for maxProfit IV --- .../0188.买卖股票的最佳时机IV.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 431c292b..8d9c8657 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -230,6 +230,31 @@ class Solution: Go: +Javascript: + +```javascript +const maxProfit = (k,prices) => { + if (prices == null || prices.length < 2 || k == 0) { + return 0; + } + + let dp = Array.from(Array(prices.length), () => Array(2*k+1).fill(0)); + + for (let j = 1; j < 2 * k; j += 2) { + dp[0][j] = 0 - prices[0]; + } + + for(let i = 1; i < prices.length; i++) { + for (let j = 0; j < 2 * k; j += 2) { + dp[i][j+1] = Math.max(dp[i-1][j+1], dp[i-1][j] - prices[i]); + dp[i][j+2] = Math.max(dp[i-1][j+2], dp[i-1][j+1] + prices[i]); + } + } + + return dp[prices.length - 1][2 * k]; +}; +``` + ----------------------- From 633de2413cb2570cde71ad4091152c86f2672da1 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 20:10:53 -0700 Subject: [PATCH 10/28] =?UTF-8?q?Update=200188.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAIV.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0188.买卖股票的最佳时机IV.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 8d9c8657..0470e528 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -256,7 +256,6 @@ const maxProfit = (k,prices) => { ``` - ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 486fff2d02e41337928d109b612c0fa1487ffe30 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 20:11:13 -0700 Subject: [PATCH 11/28] =?UTF-8?q?Update=200188.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAIV.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0188.买卖股票的最佳时机IV.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 0470e528..c34a10b7 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -255,7 +255,6 @@ const maxProfit = (k,prices) => { }; ``` - ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From e267f101eea3c2bc2905e60d4472155fcab4b7b9 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 20:56:24 -0700 Subject: [PATCH 12/28] add js solution for maxProfit with Cooldown --- ...09.最佳买卖股票时机含冷冻期.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 4667c122..e28e8369 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -207,7 +207,29 @@ class Solution: Go: +Javascript: +```javascript +const maxProfit = (prices) => { + if(prices.length < 2) { + return 0 + } else if(prices.length < 3) { + return Math.max(0, prices[1] - prices[0]); + } + + let dp = Array.from(Array(prices.length), () => Array(4).fill(0)); + dp[0][0] = 0 - prices[0]; + + for(i = 1; i < prices.length; i++) { + dp[i][0] = Math.max(dp[i - 1][0], Math.max(dp[i-1][1], dp[i-1][3]) - prices[i]); + dp[i][1] = Math.max(dp[i -1][1], dp[i - 1][3]); + dp[i][2] = dp[i-1][0] + prices[i]; + dp[i][3] = dp[i-1][2]; + } + + return Math.max(dp[prices.length - 1][1], dp[prices.length - 1][2], dp[prices.length - 1][3]); +}; +``` ----------------------- From 1c583ae1df9fc17a9cf21f4090e57e526c2a729c Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 22:13:19 -0700 Subject: [PATCH 13/28] add js solution for maxProfit with fee --- ...的最佳时机含手续费(动态规划).md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 5eb3453b..5cb5d7ac 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -153,7 +153,18 @@ class Solution: Go: - +Javascript: +```javascript +const maxProfit5 = (prices,fee) => { + let dp = Array.from(Array(prices.length), () => Array(2).fill(0)); + dp[0][0] = 0 - prices[0]; + for (let i = 1; i < prices.length; i++) { + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + dp[i][1] = Math.max(dp[i - 1][0] + prices[i] - fee, dp[i - 1][1]); + } + return Math.max(dp[prices.length - 1][0], dp[prices.length - 1][1]); +} +``` ----------------------- From 69e54d9091a285eee19eb5928ee4a0ee284e8f15 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Fri, 9 Jul 2021 22:13:43 -0700 Subject: [PATCH 14/28] =?UTF-8?q?Update=200714.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E6=89=8B=E7=BB=AD=E8=B4=B9=EF=BC=88=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...买卖股票的最佳时机含手续费(动态规划).md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 5cb5d7ac..e2b83999 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -155,7 +155,7 @@ Go: Javascript: ```javascript -const maxProfit5 = (prices,fee) => { +const maxProfit = (prices,fee) => { let dp = Array.from(Array(prices.length), () => Array(2).fill(0)); dp[0][0] = 0 - prices[0]; for (let i = 1; i < prices.length; i++) { From be87481db3e7a4750daedc9a470498568cf39942 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 10 Jul 2021 14:52:36 +0800 Subject: [PATCH 15/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200047.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97II=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 0047.全排列II go版本 --- problems/0047.全排列II.md | 40 +++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index 079ca834..195ba796 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -258,7 +258,45 @@ var permuteUnique = function (nums) { }; ``` - +Go: +回溯+本层去重+下层去重 +```golang +func permuteUnique(nums []int) [][]int { + var subRes []int + var res [][]int + sort.Ints(nums) + used:=make([]bool,len(nums)) + backTring(nums,subRes,&res,used) + return res +} +func backTring(nums,subRes []int,res *[][]int,used []bool){ + if len(subRes)==len(nums){ + tmp:=make([]int,len(nums)) + copy(tmp,subRes) + *res=append(*res,tmp) + return + } + // used[i - 1] == true,说明同一树支candidates[i - 1]使用过 + // used[i - 1] == false,说明同一树层candidates[i - 1]使用过 + for i:=0;i
* 时间复杂度:O(n)
*/
public boolean isBalanced(TreeNode root) {
@@ -493,7 +492,6 @@ class Solution {
return height;
}
}
-// LeetCode题解链接:https://leetcode-cn.com/problems/balanced-binary-tree/solution/110-ping-heng-er-cha-shu-di-gui-fa-bao-l-yqr3/
```
Python:
@@ -590,6 +588,7 @@ func abs(a int)int{
return a
}
```
+
JavaScript:
```javascript
var isBalanced = function(root) {
diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md
index 3504a574..197841e9 100644
--- a/problems/0337.打家劫舍III.md
+++ b/problems/0337.打家劫舍III.md
@@ -173,7 +173,7 @@ return {val2, val1};
以示例1为例,dp数组状态如下:(**注意用后序遍历的方式推导**)
-
+
**最后头结点就是 取下标0 和 下标1的最大值就是偷得的最大金钱**。
diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md
index 71af618e..6b07c258 100644
--- a/problems/0347.前K个高频元素.md
+++ b/problems/0347.前K个高频元素.md
@@ -18,18 +18,18 @@ https://leetcode-cn.com/problems/top-k-frequent-elements/
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
示例 1:
-输入: nums = [1,1,1,2,2,3], k = 2
-输出: [1,2]
+* 输入: nums = [1,1,1,2,2,3], k = 2
+* 输出: [1,2]
示例 2:
-输入: nums = [1], k = 1
-输出: [1]
+* 输入: nums = [1], k = 1
+* 输出: [1]
提示:
-你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。
-你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。
-题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的。
-你可以按任意顺序返回答案。
+* 你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。
+* 你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。
+* 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的。
+* 你可以按任意顺序返回答案。
# 思路
@@ -70,7 +70,7 @@ https://leetcode-cn.com/problems/top-k-frequent-elements/
寻找前k个最大元素流程如图所示:(图中的频率只有三个,所以正好构成一个大小为3的小顶堆,如果频率更多一些,则用这个小顶堆进行扫描)
-
+
我们来看一下C++代码:
@@ -126,10 +126,7 @@ public:
优先级队列的定义正好反过来了,可能和优先级队列的源码实现有关(我没有仔细研究),我估计是底层实现上优先队列队首指向后面,队尾指向最前面的缘故!
-
-
-
-## 其他语言版本
+# 其他语言版本
Java:
diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md
new file mode 100644
index 00000000..3980cb0e
--- /dev/null
+++ b/problems/0503.下一个更大元素II.md
@@ -0,0 +1,103 @@
+
+# 503.下一个更大元素II
+
+链接:https://leetcode-cn.com/problems/next-greater-element-ii/
+
+给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素。数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1。
+
+示例 1:
+
+* 输入: [1,2,1]
+* 输出: [2,-1,2]
+* 解释: 第一个 1 的下一个更大的数是 2;数字 2 找不到下一个更大的数;第二个 1 的下一个最大的数需要循环搜索,结果也是 2。
+
+
+# 思路
+
+做本题之前建议先做[739. 每日温度](https://mp.weixin.qq.com/s/YeQ7eE0-hZpxJfJJziq25Q) 和 [496.下一个更大元素 I](https://mp.weixin.qq.com/s/U0O6XkFOe-RMXthPS16sWQ)。
+
+这道题和[739. 每日温度](https://mp.weixin.qq.com/s/YeQ7eE0-hZpxJfJJziq25Q)也几乎如出一辙。
+
+不同的时候本题要循环数组了。
+
+关于单调栈的讲解我在题解[739. 每日温度](https://mp.weixin.qq.com/s/YeQ7eE0-hZpxJfJJziq25Q)中已经详细讲解了。
+
+本篇我侧重与说一说,如何处理循环数组。
+
+相信不少同学看到这道题,就想那我直接把两个数组拼接在一起,然后使用单调栈求下一个最大值不就行了!
+
+确实可以!
+
+讲两个nums数组拼接在一起,使用单调栈计算出每一个元素的下一个最大值,最后再把结果集即result数组resize到原数组大小就可以了。
+
+代码如下:
+
+```C++
+// 版本一
+class Solution {
+public:
+ vector
欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+ # 二叉树理论基础篇 我们要开启新的征程了,大家跟上! diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 68f17257..2b5a44dd 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -111,7 +111,7 @@ void traversal(TreeNode* cur, vector