Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 24 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
May 29, 2023
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2f86a5c
Update 0701.二叉搜索树中的插入操作.md
jianghongcheng May 25, 2023
343e077
Update 0216.组合总和III.md
jianghongcheng May 26, 2023
89e9d75
Update 0077.组合.md
jianghongcheng May 26, 2023
4d48c45
Update 0077.组合优化.md
jianghongcheng May 26, 2023
847c60a
Update 0017.电话号码的字母组合.md
jianghongcheng May 26, 2023
2c51420
Update 0039.组合总和.md
jianghongcheng May 26, 2023
20db57f
Update 0040.组合总和II.md
jianghongcheng May 26, 2023
59742e5
Update 0040.组合总和II.md
jianghongcheng May 26, 2023
4586386
Update 0131.分割回文串.md
jianghongcheng May 27, 2023
78445d7
Update 0131.分割回文串.md
jianghongcheng May 27, 2023
51c8976
Update 0093.复原IP地址.md
jianghongcheng May 27, 2023
d45c141
Update 0078.子集.md
jianghongcheng May 27, 2023
5801ae7
Update 0090.子集II.md
jianghongcheng May 27, 2023
eb72f26
Update 0491.递增子序列.md
jianghongcheng May 28, 2023
7e3fc8e
Update 0491.递增子序列.md
jianghongcheng May 28, 2023
a6c95b7
Update 0491.递增子序列.md
jianghongcheng May 28, 2023
b033a08
Update 0046.全排列.md
jianghongcheng May 28, 2023
fd2608a
Update 0047.全排列II.md
jianghongcheng May 28, 2023
19ecde0
Update 回溯算法去重问题的另一种写法.md
jianghongcheng May 28, 2023
498395d
Update 0332.重新安排行程.md
jianghongcheng May 28, 2023
93e0a18
Update 0051.N皇后.md
jianghongcheng May 28, 2023
11581df
Update 0455.分发饼干.md
jianghongcheng May 28, 2023
18a406e
Update 0376.摆动序列.md
jianghongcheng May 28, 2023
996ddbe
Merge pull request #2098 from jianghongcheng/master
youngyangyang04 May 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0491.递增子序列.md
  • Loading branch information
jianghongcheng authored May 28, 2023
commit eb72f2637195f8c50bff7660ba90ddf217654840
106 changes: 40 additions & 66 deletions problems/0491.递增子序列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -267,79 +267,53 @@ class Solution {
### Python

python3
**回溯**
回溯 利用set去重
```python
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[:])
def findSubsequences(self, nums):
result = []
self.backtracking(nums, 0, [], result)
return result

def backtracking(self, nums, startIndex, path, result):
if len(path) > 1:
result.append(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()
used = set() # 使用集合来进行去重操作
for i in range(startIndex, len(nums)):
if path and nums[i] < path[-1]:
continue # 如果当前元素小于上一个元素,则跳过当前元素

if nums[i] in used:
continue # 如果当前元素已经使用过,则跳过当前元素

used.add(nums[i]) # 标记当前元素已经使用过
self.backtracking(nums, i + 1, path + [nums[i]], result)

```
**回溯+哈希表去重**
回溯 利用哈希表去重
```python
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[:])
def findSubsequences(self, nums):
result = []
path = []
self.backtracking(nums, 0, path, result)
return result

def backtracking(self, nums, startIndex, path, result):
if len(path) > 1:
result.append(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()
used = [0] * 201 # 使用数组来进行去重操作,题目说数值范围[-100, 100]
for i in range(startIndex, len(nums)):
if (path and nums[i] < path[-1]) or used[nums[i] + 100] == 1:
continue # 如果当前元素小于上一个元素,或者已经使用过当前元素,则跳过当前元素

used[nums[i] + 100] = 1 # 标记当前元素已经使用过
path.append(nums[i]) # 将当前元素加入当前递增子序列
self.backtracking(nums, i + 1, path, result)
path.pop()

```
### Go

Expand Down

AltStyle によって変換されたページ (->オリジナル) /