Merge branch 'youngyangyang04:master' into master

This commit is contained in:
xllpiupiu
2021-05-26 20:04:04 +08:00
13 changed files with 181 additions and 56 deletions

View File

@ -305,7 +305,8 @@
背包问题系列: 背包问题系列:
<img src='https://img-blog.csdnimg.cn/202102261550480.png' width=500 alt='背包问题大纲'> </img></div> <img src='https://code-thinking.cdn.bcebos.com/pics/动态规划-背包问题总结.png' width=500 alt='背包问题大纲'> </img></div>
11. [动态规划关于01背包问题你该了解这些](./problems/背包理论基础01背包-1.md) 11. [动态规划关于01背包问题你该了解这些](./problems/背包理论基础01背包-1.md)
12. [动态规划关于01背包问题你该了解这些滚动数组](./problems/背包理论基础01背包-2.md) 12. [动态规划关于01背包问题你该了解这些滚动数组](./problems/背包理论基础01背包-2.md)
@ -334,7 +335,8 @@
股票系列: 股票系列:
<img src='https://code-thinking.cdn.bcebos.com/pics/%E8%82%A1%E7%A5%A8%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93.jpg' width=500 alt='股票问题总结'> </img></div> <img src='https://code-thinking.cdn.bcebos.com/pics/股票问题总结.jpg' width=500 alt='股票问题总结'> </img></div>
32. [动态规划:买卖股票的最佳时机](./problems/0121.买卖股票的最佳时机.md) 32. [动态规划:买卖股票的最佳时机](./problems/0121.买卖股票的最佳时机.md)
33. [动态规划:本周我们都讲了这些(系列六)](./problems/周总结/20210225动规周末总结.md) 33. [动态规划:本周我们都讲了这些(系列六)](./problems/周总结/20210225动规周末总结.md)
@ -348,6 +350,9 @@
子序列系列: 子序列系列:
<img src='https://code-thinking.cdn.bcebos.com/pics/动态规划-子序列问题总结.jpg' width=500 alt=''> </img></div>
40. [动态规划:最长递增子序列](./problems/0300.最长上升子序列.md) 40. [动态规划:最长递增子序列](./problems/0300.最长上升子序列.md)
41. [动态规划:最长连续递增序列](./problems/0674.最长连续递增序列.md) 41. [动态规划:最长连续递增序列](./problems/0674.最长连续递增序列.md)
42. [动态规划:最长重复子数组](./problems/0718.最长重复子数组.md) 42. [动态规划:最长重复子数组](./problems/0718.最长重复子数组.md)

View File

@ -57,7 +57,9 @@ std::unordered_map 底层实现为哈希表std::map 和std::multimap 的底
解题思路动画如下: 解题思路动画如下:
<video src='https://code-thinking.cdn.bcebos.com/gifs/1.%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.mp4' controls='controls' width='640' height='320' autoplay='autoplay'> Your browser does not support the video tag.</video></div>
![](https://code-thinking.cdn.bcebos.com/gifs/1.两数之和.gif)
C++代码: C++代码:

View File

@ -282,6 +282,43 @@ class Solution {
``` ```
Python 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 ```python3
class Solution: class Solution:
def letterCombinations(self, digits: str) -> List[str]: def letterCombinations(self, digits: str) -> List[str]:
@ -302,6 +339,7 @@ class Solution:
return res return res
``` ```
Go Go

View File

@ -190,7 +190,23 @@ func maxSubArray(nums []int) int {
return maxSum 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
};
```

View File

@ -292,7 +292,24 @@ class Solution {
``` ```
Python 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 Go

View File

@ -36,7 +36,7 @@ https://leetcode-cn.com/problems/happy-number/
题目中说了会 **无限循环**,那么也就是说**求和的过程中sum会重复出现这对解题很重要** 题目中说了会 **无限循环**,那么也就是说**求和的过程中sum会重复出现这对解题很重要**
正如:[关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA)中所说,**当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法了。** 正如:[关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/RSUANESA_tkhKhYe3ZR8Jg)中所说,**当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法了。**
所以这道题目使用哈希法来判断这个sum是否重复出现如果重复了就是return false 否则一直找到sum为1为止。 所以这道题目使用哈希法来判断这个sum是否重复出现如果重复了就是return false 否则一直找到sum为1为止。
@ -80,7 +80,7 @@ public:
## 其他语言版本 # 其他语言版本
Java Java

View File

@ -15,7 +15,18 @@ https://leetcode-cn.com/problems/remove-linked-list-elements/
题意:删除链表中等于给定值 val 的所有节点。 题意:删除链表中等于给定值 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
```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: javaScript:
```js ```js

View File

@ -116,26 +116,6 @@ public:
不要以为for里放一个while就以为是$O(n^2)$啊, 主要是看每一个元素被操作的次数每个元素在滑动窗后进来操作一次出去操作一次每个元素都是被被操作两次所以时间复杂度是2 * n 也就是$O(n)$。 不要以为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.水果成篮 * 904.水果成篮
@ -170,6 +150,22 @@ class Solution {
Python 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
```go ```go

View File

@ -118,6 +118,17 @@ class Solution {
``` ```
Python 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 Go

View File

@ -143,7 +143,23 @@ Python
Go 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) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

View File

@ -21,8 +21,8 @@
输入nums: [1, 1, 1, 1, 1], S: 3 输入nums: [1, 1, 1, 1, 1], S: 3
输出5 输出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

View File

@ -265,7 +265,9 @@ class Solution {
``` ```
Python Python
```python3 ```python3
# Definition for a binary tree node. # Definition for a binary tree node.
# class TreeNode: # class TreeNode:
# def __init__(self, val=0, left=None, right=None): # def __init__(self, val=0, left=None, right=None):

View File

@ -95,18 +95,6 @@
## 其他语言版本
Java
Python
Go
----------------------- -----------------------