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 01/13] =?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 02/13] 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 03/13] =?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 04/13] 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 05/13] =?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 06/13] =?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 07/13] 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 08/13] 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 09/13] =?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 10/13] =?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;i0&&nums[i]==nums[i-1]&&used[i-1]==false{//当本层元素相同且前一个被使用过,则继续向后找(本层去重) + continue + } + //到达这里有两种情况:1.该层前后元素不同;2.该层前后元素相同但该层没有使用过 + //所以只能对该层没有被使用过的抽取 + if used[i]==false{ + //首先将该元素置为使用过(即同一树枝使用过),下一层的元素就不能选择重复使用过的元素(下层去重) + used[i]=true + subRes=append(subRes,nums[i]) + backTring(nums,subRes,res,used) + //回溯 + //回溯回来,将该元素置为false,表示该元素在该层使用过 + used[i]=false + subRes=subRes[:len(subRes)-1] + } + } +} +``` From 219d1d9982bd57dd1249abc1c08f049215104ca1 Mon Sep 17 00:00:00 2001 From: hk27xing <244798299@qq.com> Date: Sat, 10 Jul 2021 21:09:42 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E6=94=B9=E8=BF=9B718.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0718.最长重复子数组.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md index bef616d3..9ee94a3d 100644 --- a/problems/0718.最长重复子数组.md +++ b/problems/0718.最长重复子数组.md @@ -20,7 +20,7 @@ B: [3,2,1,4,7] 输出:3 解释: 长度最长的公共子数组是 [3, 2, 1] 。 -  + 提示: * 1 <= len(A), len(B) <= 1000 @@ -155,6 +155,7 @@ public: Java: ```java +// 版本一 class Solution { public int findLength(int[] nums1, int[] nums2) { int result = 0; @@ -164,7 +165,7 @@ class Solution { for (int j = 1; j < nums2.length + 1; j++) { if (nums1[i - 1] == nums2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; - max = Math.max(max, dp[i][j]); + result = Math.max(result, dp[i][j]); } } } @@ -172,6 +173,26 @@ class Solution { return result; } } + +// 版本二: 滚动数组 +class Solution { + public int findLength(int[] nums1, int[] nums2) { + int[] dp = new int[nums2.length + 1]; + int result = 0; + + for (int i = 1; i <= nums1.length; i++) { + for (int j = nums2.length; j > 0; j--) { + if (nums1[i - 1] == nums2[j - 1]) { + dp[j] = dp[j - 1] + 1; + } else { + dp[j] = 0; + } + result = Math.max(result, dp[j]); + } + } + return result; + } +} ``` Python: From fba61466482ff32021f9775ee99b96b2a8a73305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=A4=E9=9C=B2?= <080301087@163.com> Date: Mon, 12 Jul 2021 11:35:45 +0800 Subject: [PATCH 12/13] =?UTF-8?q?Update=200501.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0.md=20-?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E4=BA=86python3=E7=89=88=E6=9C=AC?= =?UTF-8?q?=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 --- problems/0501.二叉搜索树中的众数.md | 62 +++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 66be790f..ac7ff603 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -435,7 +435,7 @@ Python: # self.val = val # self.left = left # self.right = right -//递归法 +# 递归法 class Solution: def findMode(self, root: TreeNode) -> List[int]: if not root: return @@ -460,6 +460,66 @@ class Solution: return findNumber(root) return self.res + + +# 迭代法-中序遍历-使用额外空间map的方法: +class Solution: + def findMode(self, root: TreeNode) -> List[int]: + stack = [] + cur = root + pre = None + dist = {} + while cur or stack: + if cur: # 指针来访问节点,访问到最底层 + stack.append(cur) + cur = cur.left + else: # 逐一处理节点 + cur = stack.pop() + if cur.val in dist: + dist[cur.val] += 1 + else: + dist[cur.val] = 1 + pre = cur + cur = cur.right + + # 找出字典中最大的key + res = [] + for key, value in dist.items(): + if (value == max(dist.values())): + res.append(key) + return res + +# 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性: +class Solution: + def findMode(self, root: TreeNode) -> List[int]: + stack = [] + cur = root + pre = None + maxCount, count = 0, 0 + res = [] + while cur or stack: + if cur: # 指针来访问节点,访问到最底层 + stack.append(cur) + cur = cur.left + else: # 逐一处理节点 + cur = stack.pop() + if pre == None: # 第一个节点 + count = 1 + elif pre.val == cur.val: # 与前一个节点数值相同 + count += 1 + else: + count = 1 + if count == maxCount: + res.append(cur.val) + if count > maxCount: + maxCount = count + res.clear() + res.append(cur.val) + + pre = cur + cur = cur.right + return res + ``` Go: 暴力法(非BSL) From 42c62eeae01b837ddd9bc9e8eaaba494a91813d3 Mon Sep 17 00:00:00 2001 From: ltinyho Date: Mon, 12 Jul 2021 23:13:03 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=A2=98=E5=8F=B7?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index d798ca90..ae9a9267 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -13,8 +13,8 @@ 接下来我通过详细讲解如下两道题,来回答这个问题: -* 112. 路径总和 -* 113. 路径总和II +* 112.路径总和 +* 113.路径总和II ## 112. 路径总和