From 9f02bcd63d21adcc0aa2d6e617155d12a3837075 Mon Sep 17 00:00:00 2001 From: Chen-Wang-JY <75002576+Chen-Wang-JY@users.noreply.github.com> Date: Sun, 23 May 2021 15:58:26 +0800 Subject: [PATCH 01/10] =?UTF-8?q?Update=200017.=E7=94=B5=E8=AF=9D=E5=8F=B7?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 35 +++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index f06ed80a..b35780ae 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -281,7 +281,40 @@ class Solution { } ``` -Python: +Python3: +```Python3 +class Solution: + ans = [] + s = '' + letterMap = { + '2': 'abc', + '3': 'def', + '4': 'ghi', + '5': 'jkl', + '6': 'mno', + '7': 'pqrs', + '8': 'tuv', + '9': 'wxyz' + } + + def letterCombinations(self, digits): + self.ans.clear() + if digits == '': + return self.ans + self.backtracking(digits, 0) + return self.ans + + def backtracking(self, digits, index): + if index == len(digits): + self.ans.append(self.s) + return + else: + letters = self.letterMap[digits[index]] # 取出数字对应的字符集 + for letter in letters: + self.s = self.s + letter # 处理 + self.backtracking(digits, index + 1) + self.s = self.s[:-1] # 回溯 +``` Go: From 6f97645386b58cf31f9a84adc02505ab44b77d84 Mon Sep 17 00:00:00 2001 From: Chen-Wang-JY <75002576+Chen-Wang-JY@users.noreply.github.com> Date: Mon, 24 May 2021 18:21:14 +0800 Subject: [PATCH 02/10] =?UTF-8?q?Create=200017.=E7=94=B5=E8=AF=9D=E5=8F=B7?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index b35780ae..de1276fb 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -281,8 +281,8 @@ class Solution { } ``` -Python3: -```Python3 +Python: +```Python class Solution: ans = [] s = '' From ed8ca0daae4920af17410fc4724a207cb0eedd95 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Tue, 25 May 2021 15:14:00 +0200 Subject: [PATCH 03/10] =?UTF-8?q?Update=200131.=E5=88=86=E5=89=B2=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0131.分割回文串 python3版本 --- problems/0131.分割回文串.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 9b2401de..c722af37 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -292,7 +292,24 @@ class Solution { ``` Python: - +```python3 +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + path = [] #放已经回文的子串 + def backtrack(s,startIndex): + if startIndex >= len(s): #如果起始位置已经大于s的大小,说明已经找到了一组分割方案了 + return res.append(path[:]) + for i in range(startIndex,len(s)): + p = s[startIndex:i+1] #获取[startIndex,i+1]在s中的子串 + if p == p[::-1]: path.append(p) #是回文子串 + else: continue #不是回文,跳过 + backtrack(s,i+1) #寻找i+1为起始位置的子串 + path.pop() #回溯过程,弹出本次已经填在path的子串 + backtrack(s,0) + return res + +``` Go: From eb1d3b70d0dd56ce03e73ae42e5699bca7e0857a Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 25 May 2021 23:13:20 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A00203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 7735f6c4..abae45f4 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -201,6 +201,29 @@ Python: Go: +```go +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func removeElements(head *ListNode, val int) *ListNode { + dummyHead := &ListNode{} + dummyHead.Next = head + cur := dummyHead + for cur != nil && cur.Next != nil { + if cur.Next.Val == val { + cur.Next = cur.Next.Next + } else { + cur = cur.Next + } + } + return dummyHead.Next +} +``` + javaScript: ```js From ccfdd971b60b8090c5b9e9ad960e9fbe8a79b4a0 Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Wed, 26 May 2021 08:12:44 +0800 Subject: [PATCH 05/10] =?UTF-8?q?0376.=E6=91=86=E5=8A=A8=E5=BA=8F=E5=88=97?= =?UTF-8?q?.md=20Javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index db80a3e5..6fa30cde 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -143,7 +143,23 @@ Python: Go: - +Javascript: +```Javascript +var wiggleMaxLength = function(nums) { + if(nums.length <= 1) return nums.length + let result = 1 + let preDiff = 0 + let curDiff = 0 + for(let i = 0; i <= nums.length; i++) { + curDiff = nums[i + 1] - nums[i] + if((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) { + result++ + preDiff = curDiff + } + } + return result +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 8d797eaa7731221cc7a3ca7aa1ddc134d9c8d0df Mon Sep 17 00:00:00 2001 From: jenliketen Date: Tue, 25 May 2021 20:14:01 -0400 Subject: [PATCH 06/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A00349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20python3?= =?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/0349.两个数组的交集.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 46e15482..fe019a0a 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -118,6 +118,17 @@ class Solution { ``` Python: +```python3 +class Solution: + def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: + result_set = set() + + set1 = set(nums1) + for num in nums2: + if num in set1: + result_set.add(num) # set1里出现的nums2元素 存放到结果 + return result_set +``` Go: From 7d15e7e852510803d264169d3ec3aa987b84c444 Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Wed, 26 May 2021 08:27:25 +0800 Subject: [PATCH 07/10] =?UTF-8?q?0053.=E6=9C=80=E5=A4=A7=E5=AD=90=E5=BA=8F?= =?UTF-8?q?=E5=92=8C.md=20Javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0053.最大子序和.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 0d47a197..81e0b35a 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -190,7 +190,23 @@ func maxSubArray(nums []int) int { return maxSum } ``` - +Javascript: +```Javascript +var maxSubArray = function(nums) { + let result = -Infinity + let count = 0 + for(let i = 0; i < nums.length; i++) { + count += nums[i] + if(count > result) { + result = count + } + if(count < 0) { + count = 0 + } + } + return result +}; +``` From 49d45cf9490f89442f9a27133dfc7ef04fb7e407 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Wed, 26 May 2021 09:44:15 +0800 Subject: [PATCH 08/10] Update --- README.md | 9 ++++-- problems/0001.两数之和.md | 2 +- problems/0202.快乐数.md | 18 ++++++------ problems/0203.移除链表元素.md | 13 +++++++- problems/0209.长度最小的子数组.md | 36 ++++++++++------------- problems/0494.目标和.md | 16 +++++----- problems/0669.修剪二叉搜索树.md | 2 ++ problems/背包总结篇.md | 12 -------- 8 files changed, 55 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 82c65c7a..05fc0627 100644 --- a/README.md +++ b/README.md @@ -305,7 +305,8 @@ 背包问题系列: -背包问题大纲 +背包问题大纲 + 11. [动态规划:关于01背包问题,你该了解这些!](./problems/背包理论基础01背包-1.md) 12. [动态规划:关于01背包问题,你该了解这些!(滚动数组)](./problems/背包理论基础01背包-2.md) @@ -334,7 +335,8 @@ 股票系列: -股票问题总结 +股票问题总结 + 32. [动态规划:买卖股票的最佳时机](./problems/0121.买卖股票的最佳时机.md) 33. [动态规划:本周我们都讲了这些(系列六)](./problems/周总结/20210225动规周末总结.md) @@ -348,6 +350,9 @@ 子序列系列: + + + 40. [动态规划:最长递增子序列](./problems/0300.最长上升子序列.md) 41. [动态规划:最长连续递增序列](./problems/0674.最长连续递增序列.md) 42. [动态规划:最长重复子数组](./problems/0718.最长重复子数组.md) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index f81c35ca..e3a64f17 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -57,7 +57,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 解题思路动画如下: - +![](https://code-thinking.cdn.bcebos.com/gifs/1.两数之和.mp4) C++代码: diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 06a6914c..c07405ec 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -22,13 +22,13 @@ https://leetcode-cn.com/problems/happy-number/ **示例:** -输入:19 -输出:true -解释: -1^2 + 9^2 = 82 -8^2 + 2^2 = 68 -6^2 + 8^2 = 100 -1^2 + 0^2 + 0^2 = 1 +输入:19 +输出:true +解释: +1^2 + 9^2 = 82 +8^2 + 2^2 = 68 +6^2 + 8^2 = 100 +1^2 + 0^2 + 0^2 = 1 # 思路 @@ -36,7 +36,7 @@ https://leetcode-cn.com/problems/happy-number/ 题目中说了会 **无限循环**,那么也就是说**求和的过程中,sum会重复出现,这对解题很重要!** -正如:[关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA)中所说,**当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法了。** +正如:[关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/RSUANESA_tkhKhYe3ZR8Jg)中所说,**当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法了。** 所以这道题目使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。 @@ -80,7 +80,7 @@ public: -## 其他语言版本 +# 其他语言版本 Java: diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index abae45f4..dce5d265 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -15,7 +15,18 @@ https://leetcode-cn.com/problems/remove-linked-list-elements/ 题意:删除链表中等于给定值 val 的所有节点。 -![203题目示例](https://img-blog.csdnimg.cn/20200814104441179.png) +示例 1: +输入:head = [1,2,6,3,4,5,6], val = 6 +输出:[1,2,3,4,5] + +示例 2: +输入:head = [], val = 1 +输出:[] + +示例 3: +输入:head = [7,7,7,7], val = 7 +输出:[] + # 思路 diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index bd25083e..90280451 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -116,26 +116,6 @@ public: 不要以为for里放一个while就以为是$O(n^2)$啊, 主要是看每一个元素被操作的次数,每个元素在滑动窗后进来操作一次,出去操作一次,每个元素都是被被操作两次,所以时间复杂度是2 * n 也就是$O(n)$。 -## 其他语言补充 - -python: - -```python -class Solution: - def minSubArrayLen(self, s: int, nums: List[int]) -> int: - # 定义一个无限大的数 - res = float("inf") - Sum = 0 - index = 0 - for i in range(len(nums)): - Sum += nums[i] - while Sum >= s: - res = min(res, i-index+1) - Sum -= nums[index] - index += 1 - return 0 if res==float("inf") else res -``` - ## 相关题目推荐 * 904.水果成篮 @@ -170,6 +150,22 @@ class Solution { Python: +```python +class Solution: + def minSubArrayLen(self, s: int, nums: List[int]) -> int: + # 定义一个无限大的数 + res = float("inf") + Sum = 0 + index = 0 + for i in range(len(nums)): + Sum += nums[i] + while Sum >= s: + res = min(res, i-index+1) + Sum -= nums[index] + index += 1 + return 0 if res==float("inf") else res +``` + Go: ```go diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 84f99b1d..a9ddc768 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -19,15 +19,15 @@ 示例: -输入:nums: [1, 1, 1, 1, 1], S: 3 -输出:5 -解释: +输入:nums: [1, 1, 1, 1, 1], S: 3 +输出:5 --1+1+1+1+1 = 3 -+1-1+1+1+1 = 3 -+1+1-1+1+1 = 3 -+1+1+1-1+1 = 3 -+1+1+1+1-1 = 3 +解释: +-1+1+1+1+1 = 3 ++1-1+1+1+1 = 3 ++1+1-1+1+1 = 3 ++1+1+1-1+1 = 3 ++1+1+1+1-1 = 3 一共有5种方法让最终目标和为3。 diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 0b49bea2..06d99b9d 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -265,7 +265,9 @@ class Solution { ``` Python: + ```python3 + # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): diff --git a/problems/背包总结篇.md b/problems/背包总结篇.md index c04dfbec..0dd407ac 100644 --- a/problems/背包总结篇.md +++ b/problems/背包总结篇.md @@ -95,18 +95,6 @@ -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - ----------------------- From 4a00533907431b71d538af6a92581621b71fe2c6 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Wed, 26 May 2021 09:51:53 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E6=8F=92=E5=85=A5=E8=A7=86=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index e3a64f17..d8a57055 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -57,7 +57,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 解题思路动画如下: -![](https://code-thinking.cdn.bcebos.com/gifs/1.两数之和.mp4) + C++代码: From b2c04ffbcad54cb0f4521df62edfcf1439012204 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Wed, 26 May 2021 10:04:01 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index d8a57055..77318294 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -57,7 +57,9 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 解题思路动画如下: - + +![](https://code-thinking.cdn.bcebos.com/gifs/1.两数之和.gif) + C++代码: