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..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++代码: diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 0a69c69c..fa135d8d 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -282,6 +282,43 @@ class Solution { ``` Python: + +```Python +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] # 回溯 +``` + +python3: + ```python3 class Solution: def letterCombinations(self, digits: str) -> List[str]: @@ -302,6 +339,7 @@ class Solution: return res ``` + Go: 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 +}; +``` 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: 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 7735f6c4..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 +输出:[] + # 思路 @@ -201,6 +212,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 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/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: 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) 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: - - -----------------------