Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Feb 28, 2022
1 parent 266702c commit 37e7d73
Show file tree
Hide file tree
Showing 42 changed files with 64 additions and 60 deletions.
4 changes: 2 additions & 2 deletions problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class Solution {
```

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

```python3
```python
# 方法二,使用字典
class Solution:
def isValid(self, s: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion problems/0027.移除元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Solution {

Python:

```python3
```python
class Solution:
"""双指针法
时间复杂度:O(n)
Expand Down
2 changes: 1 addition & 1 deletion problems/0035.搜索插入位置.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions problems/0039.组合总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class Solution {

## Python
**回溯**
```python3
```python
class Solution:
def __init__(self):
self.path = []
Expand Down Expand Up @@ -296,7 +296,7 @@ class Solution:
self.path.pop() # 回溯
```
**剪枝回溯**
```python3
```python
class Solution:
def __init__(self):
self.path = []
Expand Down
4 changes: 2 additions & 2 deletions problems/0040.组合总和II.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class Solution {

## Python
**回溯+巧妙去重(省去使用used**
```python3
```python
class Solution:
def __init__(self):
self.paths = []
Expand Down Expand Up @@ -374,7 +374,7 @@ class Solution:
sum_ -= candidates[i] # 回溯,为了下一轮for loop
```
**回溯+去重(使用used)**
```python3
```python
class Solution:
def __init__(self):
self.paths = []
Expand Down
2 changes: 1 addition & 1 deletion problems/0042.接雨水.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion problems/0046.全排列.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class Solution:
usage_list[i] = False
```
**回溯+丢掉usage_list**
```python3
```python
class Solution:
def __init__(self):
self.path = []
Expand Down
4 changes: 2 additions & 2 deletions problems/0059.螺旋矩阵II.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ class Solution {
}
```

python:
python3:

```python3
```python
class Solution:

def generateMatrix(self, n: int) -> List[List[int]]:
Expand Down
4 changes: 2 additions & 2 deletions problems/0070.爬楼梯完全背包版本.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ class Solution {
}
```

Python
Python3


```python3
```python
class Solution:
def climbStairs(self, n: int) -> int:
dp = [0]*(n + 1)
Expand Down
2 changes: 1 addition & 1 deletion problems/0077.组合优化.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Solution {
```

Python:
```python3
```python
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res=[] #存放符合条件结果的集合
Expand Down
2 changes: 1 addition & 1 deletion problems/0078.子集.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class Solution {
```

## Python
```python3
```python
class Solution:
def __init__(self):
self.path: List[int] = []
Expand Down
4 changes: 2 additions & 2 deletions problems/0084.柱状图中最大的矩形.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ class Solution {
}
```

Python:
Python3:

```python3
```python

# 双指针;暴力解法(leetcode超时)
class Solution:
Expand Down
2 changes: 1 addition & 1 deletion problems/0093.复原IP地址.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class Solution(object):
```

python3:
```python3
```python
class Solution:
def __init__(self):
self.result = []
Expand Down
4 changes: 2 additions & 2 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public:
};
```
python代码
python3代码
```python3
```python
class Solution:
"""二叉树层序遍历迭代解法"""
Expand Down
2 changes: 1 addition & 1 deletion problems/0108.将有序数组转换为二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion problems/0110.平衡二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion problems/0129.求根到叶子节点数字之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Solution {
```

Python:
```python3
```python
class Solution:
def sumNumbers(self, root: TreeNode) -> int:
res = 0
Expand Down
4 changes: 2 additions & 2 deletions problems/0131.分割回文串.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class Solution {

## Python
**回溯+正反序判断回文串**
```python3
```python
class Solution:
def __init__(self):
self.paths = []
Expand Down Expand Up @@ -326,7 +326,7 @@ class Solution:
continue
```
**回溯+函数判断回文串**
```python3
```python
class Solution:
def __init__(self):
self.paths = []
Expand Down
2 changes: 1 addition & 1 deletion problems/0188.买卖股票的最佳时机IV.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion problems/0257.二叉树的所有路径.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ class Solution:

迭代法:

```python3
```python
from collections import deque


Expand Down
2 changes: 1 addition & 1 deletion problems/0279.完全平方数.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class Solution {

Python:

```python3
```python
class Solution:
def numSquares(self, n: int) -> int:
'''版本一,先遍历背包, 再遍历物品'''
Expand Down
2 changes: 1 addition & 1 deletion problems/0322.零钱兑换.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class Solution {
Python:
```python3
```python
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
'''版本一'''
Expand Down
6 changes: 3 additions & 3 deletions problems/0337.打家劫舍III.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ Python:

> 暴力递归
```python3
```python

# Definition for a binary tree node.
# class TreeNode:
Expand All @@ -315,7 +315,7 @@ class Solution:

> 记忆化递归
```python3
```python

# Definition for a binary tree node.
# class TreeNode:
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion problems/0376.摆动序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -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长度
Expand Down
2 changes: 1 addition & 1 deletion problems/0383.赎金信.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class Solution(object):

Python写法四:

```python3
```python
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
c1 = collections.Counter(ransomNote)
Expand Down
4 changes: 2 additions & 2 deletions problems/0404.左叶子之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class Solution {
## Python

**递归后序遍历**
```python3
```python
class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int:
if not root:
Expand All @@ -246,7 +246,7 @@ class Solution:
```

**迭代**
```python3
```python
class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int:
"""
Expand Down
2 changes: 1 addition & 1 deletion problems/0455.分发饼干.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Solution {
```

### Python
```python3
```python
class Solution:
# 思路1:优先考虑胃饼干
def findContentChildren(self, g: List[int], s: List[int]) -> int:
Expand Down
2 changes: 1 addition & 1 deletion problems/0463.岛屿的周长.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion problems/0474.一和零.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions problems/0491.递增子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class Solution {

python3
**回溯**
```python3
```python
class Solution:
def __init__(self):
self.paths = []
Expand Down Expand Up @@ -270,7 +270,7 @@ class Solution:
self.path.pop()
```
**回溯+哈希表去重**
```python3
```python
class Solution:
def __init__(self):
self.paths = []
Expand Down
11 changes: 8 additions & 3 deletions problems/0494.目标和.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]] 累加起来。

所以求组合类问题的公式,都是类似这种:

Expand Down
4 changes: 2 additions & 2 deletions problems/0496.下一个更大元素I.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 37e7d73

Please sign in to comment.