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 0040.组合总和II.md
  • Loading branch information
jianghongcheng authored May 26, 2023
commit 20db57f364270b9c6b9782b1bdaa675c6e1f7152
141 changes: 70 additions & 71 deletions problems/0040.组合总和II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -354,93 +354,92 @@ class Solution {
```

## Python
**回溯+巧妙去重(省去使用used**
回溯
```python
class Solution:
def __init__(self):
self.paths = []
self.path = []

def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
'''
类似于求三数之和,求四数之和,为了避免重复组合,需要提前进行数组排序
'''
self.paths.clear()
self.path.clear()
# 必须提前进行数组排序,避免重复
candidates.sort()
self.backtracking(candidates, target, 0, 0)
return self.paths

def backtracking(self, candidates: List[int], target: int, sum_: int, start_index: int) -> None:
# Base Case
if sum_ == target:
self.paths.append(self.path[:])
def backtracking(self, candidates, target, total, startIndex, path, result):
if total == target:
result.append(path[:])
return

# 单层递归逻辑
for i in range(start_index, len(candidates)):
# 剪枝,同39.组合总和
if sum_ + candidates[i] > target:
return

# 跳过同一树层使用过的元素
if i > start_index and candidates[i] == candidates[i-1]:

for i in range(startIndex, len(candidates)):
if i > startIndex and candidates[i] == candidates[i - 1]:
continue

sum_ += candidates[i]
self.path.append(candidates[i])
self.backtracking(candidates, target, sum_, i+1)
self.path.pop() # 回溯,为了下一轮for loop
sum_ -= candidates[i] # 回溯,为了下一轮for loop

if total + candidates[i] > target:
break

total += candidates[i]
path.append(candidates[i])
self.backtracking(candidates, target, total, i + 1, path, result)
total -= candidates[i]
path.pop()

def combinationSum2(self, candidates, target):
result = []
candidates.sort()
self.backtracking(candidates, target, 0, 0, [], result)
return result

```
**回溯+去重(使用used)**
回溯+去重(使用used)
```python
class Solution:
def __init__(self):
self.paths = []
self.path = []
self.used = []

def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
'''
类似于求三数之和,求四数之和,为了避免重复组合,需要提前进行数组排序
本题需要使用used,用来标记区别同一树层的元素使用重复情况:注意区分递归纵向遍历遇到的重复元素,和for循环遇到的重复元素,这两者的区别
'''
self.paths.clear()
self.path.clear()
self.usage_list = [False] * len(candidates)
# 必须提前进行数组排序,避免重复
candidates.sort()
self.backtracking(candidates, target, 0, 0)
return self.paths

def backtracking(self, candidates: List[int], target: int, sum_: int, start_index: int) -> None:
# Base Case
if sum_ == target:
self.paths.append(self.path[:])
def backtracking(self, candidates, target, total, startIndex, used, path, result):
if total == target:
result.append(path[:])
return

# 单层递归逻辑
for i in range(start_index, len(candidates)):
# 剪枝,同39.组合总和
if sum_ + candidates[i] > target:
return

# 检查同一树层是否出现曾经使用过的相同元素
# 若数组中前后元素值相同,但前者却未被使用(used == False),说明是for loop中的同一树层的相同元素情况
if i > 0 and candidates[i] == candidates[i-1] and self.usage_list[i-1] == False:

for i in range(startIndex, len(candidates)):
# 对于相同的数字,只选择第一个未被使用的数字,跳过其他相同数字
if i > startIndex and candidates[i] == candidates[i - 1] and not used[i - 1]:
continue

sum_ += candidates[i]
self.path.append(candidates[i])
self.usage_list[i] = True
self.backtracking(candidates, target, sum_, i+1)
self.usage_list[i] = False # 回溯,为了下一轮for loop
self.path.pop() # 回溯,为了下一轮for loop
sum_ -= candidates[i] # 回溯,为了下一轮for loop
if total + candidates[i] > target:
break

total += candidates[i]
path.append(candidates[i])
used[i] = True
self.backtracking(candidates, target, total, i + 1, used, path, result)
used[i] = False
total -= candidates[i]
path.pop()

def combinationSum2(self, candidates, target):
used = [False] * len(candidates)
result = []
candidates.sort()
self.backtracking(candidates, target, 0, 0, used, [], result)
return result

```
回溯优化
```python
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
results = []
self.combinationSumHelper(candidates, target, 0, [], results)
return results

def combinationSumHelper(self, candidates, target, index, path, results):
if target == 0:
results.append(path[:])
return
for i in range(index, len(candidates)):
if i > index and candidates[i] == candidates[i - 1]:
continue
if candidates[i] > target:
break
path.append(candidates[i])
self.combinationSumHelper(candidates, target - candidates[i], i + 1, path, results)
path.pop()
```
## Go
主要在于如何在回溯中去重

Expand Down

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