mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0491.递增子序列.md
补全python代码补充注释
This commit is contained in:
@ -229,32 +229,81 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Python:
|
||||
**回溯**
|
||||
```python3
|
||||
class Solution:
|
||||
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
|
||||
res = []
|
||||
path = []
|
||||
def backtrack(nums,startIndex):
|
||||
repeat = [] #这里使用数组来进行去重操作
|
||||
if len(path) >=2:
|
||||
res.append(path[:]) #注意这里不要加return,要取树上的节点
|
||||
for i in range(startIndex,len(nums)):
|
||||
if nums[i] in repeat:
|
||||
continue
|
||||
if len(path) >= 1:
|
||||
if nums[i] < path[-1]:
|
||||
continue
|
||||
repeat.append(nums[i]) #记录这个元素在本层用过了,本层后面不能再用了
|
||||
path.append(nums[i])
|
||||
backtrack(nums,i+1)
|
||||
path.pop()
|
||||
backtrack(nums,0)
|
||||
return res
|
||||
```
|
||||
def __init__(self):
|
||||
self.paths = []
|
||||
self.path = []
|
||||
|
||||
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
|
||||
'''
|
||||
本题求自增子序列,所以不能改变原数组顺序
|
||||
'''
|
||||
self.backtracking(nums, 0)
|
||||
return self.paths
|
||||
|
||||
def backtracking(self, nums: List[int], start_index: int):
|
||||
# 收集结果,同78.子集,仍要置于终止条件之前
|
||||
if len(self.path) >= 2:
|
||||
# 本题要求所有的节点
|
||||
self.paths.append(self.path[:])
|
||||
|
||||
# Base Case(可忽略)
|
||||
if start_index == len(nums):
|
||||
return
|
||||
|
||||
# 单层递归逻辑
|
||||
# 深度遍历中每一层都会有一个全新的usage_list用于记录本层元素是否重复使用
|
||||
usage_list = set()
|
||||
# 同层横向遍历
|
||||
for i in range(start_index, len(nums)):
|
||||
# 若当前元素值小于前一个时(非递增)或者曾用过,跳入下一循环
|
||||
if (self.path and nums[i] < self.path[-1]) or nums[i] in usage_list:
|
||||
continue
|
||||
usage_list.add(nums[i])
|
||||
self.path.append(nums[i])
|
||||
self.backtracking(nums, i+1)
|
||||
self.path.pop()
|
||||
```
|
||||
**回溯+哈希表去重**
|
||||
```python3
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.paths = []
|
||||
self.path = []
|
||||
|
||||
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
|
||||
'''
|
||||
本题求自增子序列,所以不能改变原数组顺序
|
||||
'''
|
||||
self.backtracking(nums, 0)
|
||||
return self.paths
|
||||
|
||||
def backtracking(self, nums: List[int], start_index: int):
|
||||
# 收集结果,同78.子集,仍要置于终止条件之前
|
||||
if len(self.path) >= 2:
|
||||
# 本题要求所有的节点
|
||||
self.paths.append(self.path[:])
|
||||
|
||||
# Base Case(可忽略)
|
||||
if start_index == len(nums):
|
||||
return
|
||||
|
||||
# 单层递归逻辑
|
||||
# 深度遍历中每一层都会有一个全新的usage_list用于记录本层元素是否重复使用
|
||||
usage_list = [False] * 201 # 使用列表去重,题中取值范围[-100, 100]
|
||||
# 同层横向遍历
|
||||
for i in range(start_index, len(nums)):
|
||||
# 若当前元素值小于前一个时(非递增)或者曾用过,跳入下一循环
|
||||
if (self.path and nums[i] < self.path[-1]) or usage_list[nums[i]+100] == True:
|
||||
continue
|
||||
usage_list[nums[i]+100] = True
|
||||
self.path.append(nums[i])
|
||||
self.backtracking(nums, i+1)
|
||||
self.path.pop()
|
||||
```
|
||||
Go:
|
||||
|
||||
```golang
|
||||
|
Reference in New Issue
Block a user