mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update
This commit is contained in:
@ -159,7 +159,7 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
```python
|
||||
# 方法一,仅使用栈,更省空间
|
||||
class Solution:
|
||||
def isValid(self, s: str) -> bool:
|
||||
@ -180,7 +180,7 @@ class Solution:
|
||||
return True if not stack else False
|
||||
```
|
||||
|
||||
```python3
|
||||
```python
|
||||
# 方法二,使用字典
|
||||
class Solution:
|
||||
def isValid(self, s: str) -> bool:
|
||||
|
@ -142,7 +142,7 @@ class Solution {
|
||||
|
||||
Python:
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
"""双指针法
|
||||
时间复杂度:O(n)
|
||||
|
@ -246,7 +246,7 @@ func searchInsert(nums []int, target int) int {
|
||||
```
|
||||
|
||||
### Python
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def searchInsert(self, nums: List[int], target: int) -> int:
|
||||
left, right = 0, len(nums) - 1
|
||||
|
@ -264,7 +264,7 @@ class Solution {
|
||||
|
||||
## Python
|
||||
**回溯**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.path = []
|
||||
@ -296,7 +296,7 @@ class Solution:
|
||||
self.path.pop() # 回溯
|
||||
```
|
||||
**剪枝回溯**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.path = []
|
||||
|
@ -334,7 +334,7 @@ class Solution {
|
||||
|
||||
## Python
|
||||
**回溯+巧妙去重(省去使用used**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.paths = []
|
||||
@ -374,7 +374,7 @@ class Solution:
|
||||
sum_ -= candidates[i] # 回溯,为了下一轮for loop
|
||||
```
|
||||
**回溯+去重(使用used)**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.paths = []
|
||||
|
@ -491,7 +491,7 @@ class Solution:
|
||||
return res
|
||||
```
|
||||
动态规划
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def trap(self, height: List[int]) -> int:
|
||||
leftheight, rightheight = [0]*len(height), [0]*len(height)
|
||||
|
@ -243,7 +243,7 @@ class Solution:
|
||||
usage_list[i] = False
|
||||
```
|
||||
**回溯+丢掉usage_list**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.path = []
|
||||
|
@ -188,9 +188,9 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
python:
|
||||
python3:
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
|
||||
def generateMatrix(self, n: int) -> List[List[int]]:
|
||||
|
@ -143,10 +143,10 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
Python3:
|
||||
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def climbStairs(self, n: int) -> int:
|
||||
dp = [0]*(n + 1)
|
||||
|
@ -174,7 +174,7 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def combine(self, n: int, k: int) -> List[List[int]]:
|
||||
res=[] #存放符合条件结果的集合
|
||||
|
@ -203,7 +203,7 @@ class Solution {
|
||||
```
|
||||
|
||||
## Python
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.path: List[int] = []
|
||||
|
@ -277,9 +277,9 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
Python3:
|
||||
|
||||
```python3
|
||||
```python
|
||||
|
||||
# 双指针;暴力解法(leetcode超时)
|
||||
class Solution:
|
||||
|
@ -339,7 +339,7 @@ class Solution(object):
|
||||
```
|
||||
|
||||
python3:
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.result = []
|
||||
|
@ -83,10 +83,10 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
python代码:
|
||||
python3代码:
|
||||
|
||||
|
||||
```python3
|
||||
```python
|
||||
|
||||
class Solution:
|
||||
"""二叉树层序遍历迭代解法"""
|
||||
|
@ -305,7 +305,7 @@ class Solution {
|
||||
## Python
|
||||
**递归**
|
||||
|
||||
```python3
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
|
@ -497,7 +497,7 @@ class Solution {
|
||||
## Python
|
||||
|
||||
递归法:
|
||||
```python3
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
|
@ -217,7 +217,7 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def sumNumbers(self, root: TreeNode) -> int:
|
||||
res = 0
|
||||
|
@ -289,7 +289,7 @@ class Solution {
|
||||
|
||||
## Python
|
||||
**回溯+正反序判断回文串**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.paths = []
|
||||
@ -326,7 +326,7 @@ class Solution:
|
||||
continue
|
||||
```
|
||||
**回溯+函数判断回文串**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.paths = []
|
||||
|
@ -271,7 +271,7 @@ class Solution:
|
||||
return dp[-1][2*k]
|
||||
```
|
||||
版本二
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def maxProfit(self, k: int, prices: List[int]) -> int:
|
||||
if len(prices) == 0: return 0
|
||||
|
@ -436,7 +436,7 @@ class Solution:
|
||||
|
||||
迭代法:
|
||||
|
||||
```python3
|
||||
```python
|
||||
from collections import deque
|
||||
|
||||
|
||||
|
@ -207,7 +207,7 @@ class Solution {
|
||||
|
||||
Python:
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def numSquares(self, n: int) -> int:
|
||||
'''版本一,先遍历背包, 再遍历物品'''
|
||||
|
@ -207,7 +207,7 @@ class Solution {
|
||||
|
||||
Python:
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def coinChange(self, coins: List[int], amount: int) -> int:
|
||||
'''版本一'''
|
||||
|
@ -288,7 +288,7 @@ Python:
|
||||
|
||||
> 暴力递归
|
||||
|
||||
```python3
|
||||
```python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
@ -315,7 +315,7 @@ class Solution:
|
||||
|
||||
> 记忆化递归
|
||||
|
||||
```python3
|
||||
```python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
@ -345,7 +345,7 @@ class Solution:
|
||||
```
|
||||
|
||||
> 动态规划
|
||||
```python3
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
|
@ -228,7 +228,7 @@ class Solution {
|
||||
|
||||
### Python
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def wiggleMaxLength(self, nums: List[int]) -> int:
|
||||
preC,curC,res = 0,0,1 #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度
|
||||
|
@ -209,7 +209,7 @@ class Solution(object):
|
||||
|
||||
Python写法四:
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
|
||||
c1 = collections.Counter(ransomNote)
|
||||
|
@ -229,7 +229,7 @@ class Solution {
|
||||
## Python
|
||||
|
||||
**递归后序遍历**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def sumOfLeftLeaves(self, root: TreeNode) -> int:
|
||||
if not root:
|
||||
@ -246,7 +246,7 @@ class Solution:
|
||||
```
|
||||
|
||||
**迭代**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def sumOfLeftLeaves(self, root: TreeNode) -> int:
|
||||
"""
|
||||
|
@ -146,7 +146,7 @@ class Solution {
|
||||
```
|
||||
|
||||
### Python
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
# 思路1:优先考虑胃饼干
|
||||
def findContentChildren(self, g: List[int], s: List[int]) -> int:
|
||||
|
@ -124,7 +124,7 @@ Python:
|
||||
### 解法1:
|
||||
扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def islandPerimeter(self, grid: List[List[int]]) -> int:
|
||||
|
||||
|
@ -193,7 +193,7 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
|
||||
dp = [[0] * (n + 1) for _ in range(m + 1)] # 默认初始化0
|
||||
|
@ -233,7 +233,7 @@ class Solution {
|
||||
|
||||
python3
|
||||
**回溯**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.paths = []
|
||||
@ -270,7 +270,7 @@ class Solution:
|
||||
self.path.pop()
|
||||
```
|
||||
**回溯+哈希表去重**
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.paths = []
|
||||
|
@ -160,11 +160,16 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法
|
||||
|
||||
那么只要搞到nums[i]的话,凑成dp[j]就有dp[j - nums[i]] 种方法。
|
||||
|
||||
举一个例子,nums[i] = 2: dp[3],填满背包容量为3的话,有dp[3]种方法。
|
||||
|
||||
那么只需要搞到一个2(nums[i]),有dp[3]方法可以凑齐容量为3的背包,相应的就有多少种方法可以凑齐容量为5的背包。
|
||||
例如:dp[j],j 为5,
|
||||
|
||||
那么需要把 这些方法累加起来就可以了,dp[j] += dp[j - nums[i]]
|
||||
* 已经有一个1(nums[i]) 的话,有 dp[4]种方法 凑成 dp[5]。
|
||||
* 已经有一个2(nums[i]) 的话,有 dp[3]种方法 凑成 dp[5]。
|
||||
* 已经有一个3(nums[i]) 的话,有 dp[2]中方法 凑成 dp[5]
|
||||
* 已经有一个4(nums[i]) 的话,有 dp[1]中方法 凑成 dp[5]
|
||||
* 已经有一个5 (nums[i])的话,有 dp[0]中方法 凑成 dp[5]
|
||||
|
||||
那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。
|
||||
|
||||
所以求组合类问题的公式,都是类似这种:
|
||||
|
||||
|
@ -222,8 +222,8 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
Python:
|
||||
```python3
|
||||
Python3:
|
||||
```python
|
||||
class Solution:
|
||||
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
|
||||
result = [-1]*len(nums1)
|
||||
|
@ -470,7 +470,7 @@ class Solution {
|
||||
|
||||
> 递归法
|
||||
|
||||
```python3
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
@ -515,7 +515,7 @@ class Solution:
|
||||
|
||||
|
||||
> 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def findMode(self, root: TreeNode) -> List[int]:
|
||||
stack = []
|
||||
|
@ -124,7 +124,7 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def nextGreaterElements(self, nums: List[int]) -> List[int]:
|
||||
dp = [-1] * len(nums)
|
||||
|
@ -207,7 +207,7 @@ class Solution {
|
||||
Python:
|
||||
|
||||
|
||||
```python3
|
||||
```python
|
||||
class Solution:
|
||||
def change(self, amount: int, coins: List[int]) -> int:
|
||||
dp = [0]*(amount + 1)
|
||||
|
@ -196,7 +196,7 @@ class Solution {
|
||||
## Python
|
||||
**递归**
|
||||
|
||||
```python3
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
|
@ -264,7 +264,7 @@ class Solution {
|
||||
|
||||
## Python
|
||||
**递归**
|
||||
```python3
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
|
@ -152,7 +152,6 @@ class Solution {
|
||||
|
||||
|
||||
|
||||
Python:
|
||||
|
||||
python3
|
||||
|
||||
|
@ -194,7 +194,7 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
```python
|
||||
# 方法一,使用栈,推荐!
|
||||
class Solution:
|
||||
def removeDuplicates(self, s: str) -> str:
|
||||
@ -207,7 +207,7 @@ class Solution:
|
||||
return "".join(res) # 字符串拼接
|
||||
```
|
||||
|
||||
```python3
|
||||
```python
|
||||
# 方法二,使用双指针模拟栈,如果不让用栈可以作为备选方法。
|
||||
class Solution:
|
||||
def removeDuplicates(self, s: str) -> str:
|
||||
|
@ -168,7 +168,7 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
```python
|
||||
# 前序遍历-递归-LC144_二叉树的前序遍历
|
||||
class Solution:
|
||||
def preorderTraversal(self, root: TreeNode) -> List[int]:
|
||||
|
@ -34,8 +34,8 @@ public:
|
||||
|
||||
// 此时就是:左右节点都不为空,且数值相同的情况
|
||||
// 此时才做递归,做下一层的判断
|
||||
bool outside = compare(left->left, right->right); // 左子树:左、 右子树:左 (相对于求对称二叉树,只需改一下这里的顺序)
|
||||
bool inside = compare(left->right, right->left); // 左子树:右、 右子树:右
|
||||
bool outside = compare(left->left, right->left); // 左子树:左、 右子树:左 (相对于求对称二叉树,只需改一下这里的顺序)
|
||||
bool inside = compare(left->right, right->right); // 左子树:右、 右子树:右
|
||||
bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理)
|
||||
return isSame;
|
||||
|
||||
|
@ -216,7 +216,7 @@ private static void testCompletePackAnotherWay(){
|
||||
|
||||
Python:
|
||||
|
||||
```python3
|
||||
```python
|
||||
# 先遍历物品,再遍历背包
|
||||
def test_complete_pack1():
|
||||
weight = [1, 3, 4]
|
||||
|
Reference in New Issue
Block a user