This commit is contained in:
youngyangyang04
2022-02-28 20:52:01 +08:00
parent 266702c291
commit 37e7d73ec2
42 changed files with 64 additions and 60 deletions

View File

@ -159,7 +159,7 @@ class Solution {
``` ```
Python Python
```python3 ```python
# 方法一,仅使用栈,更省空间 # 方法一,仅使用栈,更省空间
class Solution: class Solution:
def isValid(self, s: str) -> bool: def isValid(self, s: str) -> bool:
@ -180,7 +180,7 @@ class Solution:
return True if not stack else False return True if not stack else False
``` ```
```python3 ```python
# 方法二,使用字典 # 方法二,使用字典
class Solution: class Solution:
def isValid(self, s: str) -> bool: def isValid(self, s: str) -> bool:

View File

@ -142,7 +142,7 @@ class Solution {
Python Python
```python3 ```python
class Solution: class Solution:
"""双指针法 """双指针法
时间复杂度O(n) 时间复杂度O(n)

View File

@ -246,7 +246,7 @@ func searchInsert(nums []int, target int) int {
``` ```
### Python ### Python
```python3 ```python
class Solution: class Solution:
def searchInsert(self, nums: List[int], target: int) -> int: def searchInsert(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1 left, right = 0, len(nums) - 1

View File

@ -264,7 +264,7 @@ class Solution {
## Python ## Python
**回溯** **回溯**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.path = [] self.path = []
@ -296,7 +296,7 @@ class Solution:
self.path.pop() # 回溯 self.path.pop() # 回溯
``` ```
**剪枝回溯** **剪枝回溯**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.path = [] self.path = []

View File

@ -334,7 +334,7 @@ class Solution {
## Python ## Python
**回溯+巧妙去重(省去使用used** **回溯+巧妙去重(省去使用used**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.paths = [] self.paths = []
@ -374,7 +374,7 @@ class Solution:
sum_ -= candidates[i] # 回溯为了下一轮for loop sum_ -= candidates[i] # 回溯为了下一轮for loop
``` ```
**回溯+去重使用used** **回溯+去重使用used**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.paths = [] self.paths = []

View File

@ -491,7 +491,7 @@ class Solution:
return res return res
``` ```
动态规划 动态规划
```python3 ```python
class Solution: class Solution:
def trap(self, height: List[int]) -> int: def trap(self, height: List[int]) -> int:
leftheight, rightheight = [0]*len(height), [0]*len(height) leftheight, rightheight = [0]*len(height), [0]*len(height)

View File

@ -243,7 +243,7 @@ class Solution:
usage_list[i] = False usage_list[i] = False
``` ```
**回溯+丢掉usage_list** **回溯+丢掉usage_list**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.path = [] self.path = []

View File

@ -188,9 +188,9 @@ class Solution {
} }
``` ```
python: python3:
```python3 ```python
class Solution: class Solution:
def generateMatrix(self, n: int) -> List[List[int]]: def generateMatrix(self, n: int) -> List[List[int]]:

View File

@ -143,10 +143,10 @@ class Solution {
} }
``` ```
Python Python3
```python3 ```python
class Solution: class Solution:
def climbStairs(self, n: int) -> int: def climbStairs(self, n: int) -> int:
dp = [0]*(n + 1) dp = [0]*(n + 1)

View File

@ -174,7 +174,7 @@ class Solution {
``` ```
Python Python
```python3 ```python
class Solution: class Solution:
def combine(self, n: int, k: int) -> List[List[int]]: def combine(self, n: int, k: int) -> List[List[int]]:
res=[] #存放符合条件结果的集合 res=[] #存放符合条件结果的集合

View File

@ -203,7 +203,7 @@ class Solution {
``` ```
## Python ## Python
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.path: List[int] = [] self.path: List[int] = []

View File

@ -277,9 +277,9 @@ class Solution {
} }
``` ```
Python: Python3:
```python3 ```python
# 双指针暴力解法leetcode超时 # 双指针暴力解法leetcode超时
class Solution: class Solution:

View File

@ -339,7 +339,7 @@ class Solution(object):
``` ```
python3: python3:
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.result = [] self.result = []

View File

@ -83,10 +83,10 @@ public:
}; };
``` ```
python代码 python3代码:
```python3 ```python
class Solution: class Solution:
"""二叉树层序遍历迭代解法""" """二叉树层序遍历迭代解法"""

View File

@ -305,7 +305,7 @@ class Solution {
## Python ## Python
**递归** **递归**
```python3 ```python
# 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

@ -497,7 +497,7 @@ class Solution {
## Python ## Python
递归法: 递归法:
```python3 ```python
# 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

@ -217,7 +217,7 @@ class Solution {
``` ```
Python Python
```python3 ```python
class Solution: class Solution:
def sumNumbers(self, root: TreeNode) -> int: def sumNumbers(self, root: TreeNode) -> int:
res = 0 res = 0

View File

@ -289,7 +289,7 @@ class Solution {
## Python ## Python
**回溯+正反序判断回文串** **回溯+正反序判断回文串**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.paths = [] self.paths = []
@ -326,7 +326,7 @@ class Solution:
continue continue
``` ```
**回溯+函数判断回文串** **回溯+函数判断回文串**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.paths = [] self.paths = []

View File

@ -271,7 +271,7 @@ class Solution:
return dp[-1][2*k] return dp[-1][2*k]
``` ```
版本二 版本二
```python3 ```python
class Solution: class Solution:
def maxProfit(self, k: int, prices: List[int]) -> int: def maxProfit(self, k: int, prices: List[int]) -> int:
if len(prices) == 0: return 0 if len(prices) == 0: return 0

View File

@ -436,7 +436,7 @@ class Solution:
迭代法: 迭代法:
```python3 ```python
from collections import deque from collections import deque

View File

@ -207,7 +207,7 @@ class Solution {
Python Python
```python3 ```python
class Solution: class Solution:
def numSquares(self, n: int) -> int: def numSquares(self, n: int) -> int:
'''版本一,先遍历背包, 再遍历物品''' '''版本一,先遍历背包, 再遍历物品'''

View File

@ -207,7 +207,7 @@ class Solution {
Python Python
```python3 ```python
class Solution: class Solution:
def coinChange(self, coins: List[int], amount: int) -> int: def coinChange(self, coins: List[int], amount: int) -> int:
'''版本一''' '''版本一'''

View File

@ -288,7 +288,7 @@ Python
> 暴力递归 > 暴力递归
```python3 ```python
# Definition for a binary tree node. # Definition for a binary tree node.
# class TreeNode: # class TreeNode:
@ -315,7 +315,7 @@ class Solution:
> 记忆化递归 > 记忆化递归
```python3 ```python
# Definition for a binary tree node. # Definition for a binary tree node.
# class TreeNode: # class TreeNode:
@ -345,7 +345,7 @@ class Solution:
``` ```
> 动态规划 > 动态规划
```python3 ```python
# 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

@ -228,7 +228,7 @@ class Solution {
### Python ### Python
```python3 ```python
class Solution: class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int: def wiggleMaxLength(self, nums: List[int]) -> int:
preC,curC,res = 0,0,1 #题目里nums长度大于等于1当长度为1时其实到不了for循环里去所以不用考虑nums长度 preC,curC,res = 0,0,1 #题目里nums长度大于等于1当长度为1时其实到不了for循环里去所以不用考虑nums长度

View File

@ -209,7 +209,7 @@ class Solution(object):
Python写法四 Python写法四
```python3 ```python
class Solution: class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool: def canConstruct(self, ransomNote: str, magazine: str) -> bool:
c1 = collections.Counter(ransomNote) c1 = collections.Counter(ransomNote)

View File

@ -229,7 +229,7 @@ class Solution {
## Python ## Python
**递归后序遍历** **递归后序遍历**
```python3 ```python
class Solution: class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int: def sumOfLeftLeaves(self, root: TreeNode) -> int:
if not root: if not root:
@ -246,7 +246,7 @@ class Solution:
``` ```
**迭代** **迭代**
```python3 ```python
class Solution: class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int: def sumOfLeftLeaves(self, root: TreeNode) -> int:
""" """

View File

@ -146,7 +146,7 @@ class Solution {
``` ```
### Python ### Python
```python3 ```python
class Solution: class Solution:
# 思路1优先考虑胃饼干 # 思路1优先考虑胃饼干
def findContentChildren(self, g: List[int], s: List[int]) -> int: def findContentChildren(self, g: List[int], s: List[int]) -> int:

View File

@ -124,7 +124,7 @@ Python
### 解法1: ### 解法1:
扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1 从当前位置判断四边方向如果边界或者是水域证明有边界存在res矩阵的对应cell加一。 扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1 从当前位置判断四边方向如果边界或者是水域证明有边界存在res矩阵的对应cell加一。
```python3 ```python
class Solution: class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int: def islandPerimeter(self, grid: List[List[int]]) -> int:

View File

@ -193,7 +193,7 @@ class Solution {
``` ```
Python Python
```python3 ```python
class Solution: class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int: def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
dp = [[0] * (n + 1) for _ in range(m + 1)] # 默认初始化0 dp = [[0] * (n + 1) for _ in range(m + 1)] # 默认初始化0

View File

@ -233,7 +233,7 @@ class Solution {
python3 python3
**回溯** **回溯**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.paths = [] self.paths = []
@ -270,7 +270,7 @@ class Solution:
self.path.pop() self.path.pop()
``` ```
**回溯+哈希表去重** **回溯+哈希表去重**
```python3 ```python
class Solution: class Solution:
def __init__(self): def __init__(self):
self.paths = [] self.paths = []

View File

@ -160,11 +160,16 @@ dp[j] 表示填满j包括j这么大容积的包有dp[j]种方法
那么只要搞到nums[i]的话凑成dp[j]就有dp[j - nums[i]] 种方法。 那么只要搞到nums[i]的话凑成dp[j]就有dp[j - nums[i]] 种方法。
举一个例子,nums[i] = 2 dp[3]填满背包容量为3的话有dp[3]种方法。
那么只需要搞到一个2nums[i]有dp[3]方法可以凑齐容量为3的背包相应的就有多少种方法可以凑齐容量为5的背包。 例如dp[j]j 为5
那么需要把 这些方法累加起来就可以了dp[j] += dp[j - nums[i]] * 已经有一个1nums[i] 的话,有 dp[4]种方法 凑成 dp[5]。
* 已经有一个2nums[i] 的话,有 dp[3]种方法 凑成 dp[5]。
* 已经有一个3nums[i] 的话,有 dp[2]中方法 凑成 dp[5]
* 已经有一个4nums[i] 的话,有 dp[1]中方法 凑成 dp[5]
* 已经有一个5 nums[i])的话,有 dp[0]中方法 凑成 dp[5]
那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。
所以求组合类问题的公式,都是类似这种: 所以求组合类问题的公式,都是类似这种:

View File

@ -222,8 +222,8 @@ class Solution {
} }
} }
``` ```
Python Python3
```python3 ```python
class Solution: class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
result = [-1]*len(nums1) result = [-1]*len(nums1)

View File

@ -470,7 +470,7 @@ class Solution {
> 递归法 > 递归法
```python3 ```python
# 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):
@ -515,7 +515,7 @@ class Solution:
> 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性 > 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性
```python3 ```python
class Solution: class Solution:
def findMode(self, root: TreeNode) -> List[int]: def findMode(self, root: TreeNode) -> List[int]:
stack = [] stack = []

View File

@ -124,7 +124,7 @@ class Solution {
``` ```
Python: Python:
```python3 ```python
class Solution: class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]: def nextGreaterElements(self, nums: List[int]) -> List[int]:
dp = [-1] * len(nums) dp = [-1] * len(nums)

View File

@ -207,7 +207,7 @@ class Solution {
Python Python
```python3 ```python
class Solution: class Solution:
def change(self, amount: int, coins: List[int]) -> int: def change(self, amount: int, coins: List[int]) -> int:
dp = [0]*(amount + 1) dp = [0]*(amount + 1)

View File

@ -196,7 +196,7 @@ class Solution {
## Python ## Python
**递归** **递归**
```python3 ```python
# 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

@ -264,7 +264,7 @@ class Solution {
## Python ## Python
**递归** **递归**
```python3 ```python
# 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

@ -152,7 +152,6 @@ class Solution {
Python
python3 python3

View File

@ -194,7 +194,7 @@ class Solution {
``` ```
Python Python
```python3 ```python
# 方法一,使用栈,推荐! # 方法一,使用栈,推荐!
class Solution: class Solution:
def removeDuplicates(self, s: str) -> str: def removeDuplicates(self, s: str) -> str:
@ -207,7 +207,7 @@ class Solution:
return "".join(res) # 字符串拼接 return "".join(res) # 字符串拼接
``` ```
```python3 ```python
# 方法二,使用双指针模拟栈,如果不让用栈可以作为备选方法。 # 方法二,使用双指针模拟栈,如果不让用栈可以作为备选方法。
class Solution: class Solution:
def removeDuplicates(self, s: str) -> str: def removeDuplicates(self, s: str) -> str:

View File

@ -168,7 +168,7 @@ class Solution {
``` ```
Python Python
```python3 ```python
# 前序遍历-递归-LC144_二叉树的前序遍历 # 前序遍历-递归-LC144_二叉树的前序遍历
class Solution: class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]: def preorderTraversal(self, root: TreeNode) -> List[int]:

View File

@ -34,8 +34,8 @@ public:
// 此时就是:左右节点都不为空,且数值相同的情况 // 此时就是:左右节点都不为空,且数值相同的情况
// 此时才做递归,做下一层的判断 // 此时才做递归,做下一层的判断
bool outside = compare(left->left, right->right); // 左子树:左、 右子树:左 (相对于求对称二叉树,只需改一下这里的顺序) bool outside = compare(left->left, right->left); // 左子树:左、 右子树:左 (相对于求对称二叉树,只需改一下这里的顺序)
bool inside = compare(left->right, right->left); // 左子树:右、 右子树:右 bool inside = compare(left->right, right->right); // 左子树:右、 右子树:右
bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理) bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理)
return isSame; return isSame;

View File

@ -216,7 +216,7 @@ private static void testCompletePackAnotherWay(){
Python Python
```python3 ```python
# 先遍历物品,再遍历背包 # 先遍历物品,再遍历背包
def test_complete_pack1(): def test_complete_pack1():
weight = [1, 3, 4] weight = [1, 3, 4]