From 1d64c61373b7187a5aa5e27cab49c65c522b2928 Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Mon, 9 Aug 2021 14:20:32 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A00209.=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84=20Swfit?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0209.长度最小的子数组.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 42687514..3a593f6e 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -216,6 +216,29 @@ var minSubArrayLen = function(target, nums) { }; ``` +Swift: + +```swift +func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int { + var result = Int.max + var sum = 0 + var starIndex = 0 + for endIndex in 0..= target { + result = min(result, endIndex - starIndex + 1) + sum -= nums[starIndex] + starIndex += 1 + } + } + + return result == Int.max ? 0 : result +} +``` + + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 9aa4cbbe947f1a04a9026dd860adaf4e9580e524 Mon Sep 17 00:00:00 2001 From: zwe <52469565+zhou-waner@users.noreply.github.com> Date: Mon, 9 Aug 2021 18:18:10 -0400 Subject: [PATCH 2/7] =?UTF-8?q?Update=20=E5=93=88=E5=B8=8C=E8=A1=A8?= =?UTF-8?q?=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typo: 下表->下标 --- problems/哈希表总结.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/哈希表总结.md b/problems/哈希表总结.md index c3fbde2b..e97a0874 100644 --- a/problems/哈希表总结.md +++ b/problems/哈希表总结.md @@ -84,7 +84,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 来说一说:使用数组和set来做哈希法的局限。 * 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。 -* set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下表位置,因为要返回x 和 y的下表。所以set 也不能用。 +* set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下标位置,因为要返回x 和 y的下标。所以set 也不能用。 map是一种``的结构,本题可以用key保存数值,用value在保存数值所在的下表。所以使用map最为合适。 From 7a6bf94cfac1a80f137f054732491b0592855eae Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Tue, 10 Aug 2021 07:10:19 +0800 Subject: [PATCH 3/7] =?UTF-8?q?0279.=E5=AE=8C=E5=85=A8=E5=B9=B3=E6=96=B9?= =?UTF-8?q?=E6=95=B0=20Javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index d0922de1..e3eb6f8c 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -286,7 +286,35 @@ func min(a, b int) int { } ``` +Javascript: +```Javascript +// 先遍历物品,再遍历背包 +var numSquares1 = function(n) { + let dp = new Array(n + 1).fill(Infinity) + dp[0] = 0 + for(let i = 0; i <= n; i++) { + let val = i * i + for(let j = val; j <= n; j++) { + dp[j] = Math.min(dp[j], dp[j - val] + 1) + } + } + return dp[n] +}; +// 先遍历背包,再遍历物品 +var numSquares2 = function(n) { + let dp = new Array(n + 1).fill(Infinity) + dp[0] = 0 + + for(let i = 1; i <= n; i++) { + for(let j = 1; j * j <= i; j++) { + dp[i] = Math.min(dp[i - j * j] + 1, dp[i]) + } + } + + return dp[n] +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 64b4a590729606a9466465a2e20f3685f9ac5d85 Mon Sep 17 00:00:00 2001 From: wjjiang <48505670+Spongecaptain@users.noreply.github.com> Date: Tue, 10 Aug 2021 10:17:59 +0800 Subject: [PATCH 4/7] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 Go 语言迭代版本 --- problems/0669.修剪二叉搜索树.md | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 26394eaa..c13af226 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -296,6 +296,7 @@ Go: * Right *TreeNode * } */ +// 递归 func trimBST(root *TreeNode, low int, high int) *TreeNode { if root==nil{ return nil @@ -312,6 +313,37 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode { root.Right=trimBST(root.Right,low,high) return root } +// 迭代 +func trimBST(root *TreeNode, low int, high int) *TreeNode { + if root == nil { + return nil + } + // 处理 root,让 root 移动到[low, high] 范围内,注意是左闭右闭 + for root != nil && (root.Valhigh){ + if root.Val < low{ + root = root.Right + }else{ + root = root.Left + } + } + cur := root + // 此时 root 已经在[low, high] 范围内,处理左孩子元素小于 low 的情况(左节点是一定小于 root.Val,因此天然小于 high) + for cur != nil{ + for cur.Left!=nil && cur.Left.Val < low{ + cur.Left = cur.Left.Right + } + cur = cur.Left + } + cur = root + // 此时 root 已经在[low, high] 范围内,处理右孩子大于 high 的情况 + for cur != nil{ + for cur.Right!=nil && cur.Right.Val > high{ + cur.Right = cur.Right.Left + } + cur = cur.Right + } + return root +} ``` From 6f7ffaa08755564c6a5714445e8861386fe3869f Mon Sep 17 00:00:00 2001 From: caozheng0401 <1546376420@qq.com> Date: Tue, 10 Aug 2021 15:31:10 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20496.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0=20Java=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0496.下一个更大元素I.md | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 0404e434..566a09b1 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -186,7 +186,37 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 +Java +```java +class Solution { + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + Stack temp = new Stack<>(); + int[] res = new int[nums1.length]; + Arrays.fill(res,-1); + HashMap hashMap = new HashMap<>(); + for (int i = 0 ; i< nums1.length ; i++){ + hashMap.put(nums1[i],i); + } + temp.add(0); + for (int i = 1; i < nums2.length; i++) { + if (nums2[i] <= nums2[temp.peek()]) { + temp.add(i); + } else { + while (!temp.isEmpty() && nums2[temp.peek()] < nums2[i]) { + if (hashMap.containsKey(nums2[temp.peek()])){ + Integer index = hashMap.get(nums2[temp.peek()]); + res[index] = nums2[i]; + } + temp.pop(); + } + temp.add(i); + } + } + return res; + } +} +``` Python: ```python3 class Solution: From 5f6d5a0731968ee55db47b61c1d5654751df10a0 Mon Sep 17 00:00:00 2001 From: caozheng0401 <1546376420@qq.com> Date: Tue, 10 Aug 2021 15:52:27 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=20Java=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0503.下一个更大元素II.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 34ade48e..6e351f27 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -95,7 +95,23 @@ public: ## 其他语言版本 Java: +```java +class Solution { + public int[] nextGreaterElements(int[] nums) { + int[] res = new int[nums.length]; + Arrays.fill(res, -1); + Stack temp = new Stack<>(); + for (int i = 1; i < nums.length * 2; i++) { + while (!temp.isEmpty() && nums[temp.peek()] < nums[i % nums.length]) { + res[temp.pop()] = nums[i % nums.length]; + } + temp.add(i % nums.length); + } + return res; + } +} +``` Python: ```python3 class Solution: From 811082be57e4ad2f0c94933921cb822c501ae4f1 Mon Sep 17 00:00:00 2001 From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com> Date: Tue, 10 Aug 2021 19:10:16 +0800 Subject: [PATCH 7/7] =?UTF-8?q?Update=200005.=E6=9C=80=E9=95=BF=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E5=AD=90=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added python version code --- problems/0005.最长回文子串.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 0063b358..803170f6 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -270,6 +270,23 @@ public: ## Python ```python +class Solution: + def longestPalindrome(self, s: str) -> str: + dp = [[False] * len(s) for _ in range(len(s))] + maxlenth = 0 + left = 0 + right = 0 + for i in range(len(s) - 1, -1, -1): + for j in range(i, len(s)): + if s[j] == s[i]: + if j - i <= 1 or dp[i + 1][j - 1]: + dp[i][j] = True + if dp[i][j] and j - i + 1 > maxlenth: + maxlenth = j - i + 1 + left = i + right = j + return s[left:right + 1] + ``` ## Go