mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Update 0090.子集II.md
This commit is contained in:
@ -235,86 +235,84 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Python
|
|
||||||
```python
|
|
||||||
class Solution:
|
|
||||||
def __init__(self):
|
|
||||||
self.paths = []
|
|
||||||
self.path = []
|
|
||||||
|
|
||||||
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
|
|
||||||
nums.sort()
|
|
||||||
self.backtracking(nums, 0)
|
|
||||||
return self.paths
|
|
||||||
|
|
||||||
def backtracking(self, nums: List[int], start_index: int) -> None:
|
|
||||||
# ps.空集合仍符合要求
|
|
||||||
self.paths.append(self.path[:])
|
|
||||||
# Base Case
|
|
||||||
if start_index == len(nums):
|
|
||||||
return
|
|
||||||
|
|
||||||
# 单层递归逻辑
|
|
||||||
for i in range(start_index, len(nums)):
|
|
||||||
if i > start_index and nums[i] == nums[i-1]:
|
|
||||||
# 当前后元素值相同时,跳入下一个循环,去重
|
|
||||||
continue
|
|
||||||
self.path.append(nums[i])
|
|
||||||
self.backtracking(nums, i+1)
|
|
||||||
self.path.pop()
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Python3
|
#### Python3
|
||||||
|
|
||||||
不使用used数组
|
回溯 利用used数组去重
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
|
def subsetsWithDup(self, nums):
|
||||||
res = []
|
|
||||||
path = []
|
|
||||||
nums.sort() # 去重需要先对数组进行排序
|
|
||||||
|
|
||||||
def backtracking(nums, startIndex):
|
|
||||||
# 终止条件
|
|
||||||
res.append(path[:])
|
|
||||||
if startIndex == len(nums):
|
|
||||||
return
|
|
||||||
|
|
||||||
# for循环
|
|
||||||
for i in range(startIndex, len(nums)):
|
|
||||||
# 数层去重
|
|
||||||
if i > startIndex and nums[i] == nums[i-1]: # 去重
|
|
||||||
continue
|
|
||||||
path.append(nums[i])
|
|
||||||
backtracking(nums, i+1)
|
|
||||||
path.pop()
|
|
||||||
|
|
||||||
backtracking(nums, 0)
|
|
||||||
return res
|
|
||||||
```
|
|
||||||
|
|
||||||
使用used数组
|
|
||||||
```python
|
|
||||||
class Solution:
|
|
||||||
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
|
|
||||||
result = []
|
result = []
|
||||||
path = []
|
path = []
|
||||||
nums.sort()
|
used = [False] * len(nums)
|
||||||
used = [0] * len(nums)
|
nums.sort() # 去重需要排序
|
||||||
def backtrack(nums, startIdx):
|
self.backtracking(nums, 0, used, path, result)
|
||||||
result.append(path[:])
|
|
||||||
for i in range(startIdx, len(nums)):
|
|
||||||
if i > startIdx and nums[i] == nums[i-1] and used[i-1] == 0:
|
|
||||||
continue
|
|
||||||
used[i] = 1
|
|
||||||
path.append(nums[i])
|
|
||||||
backtrack(nums, i+1)
|
|
||||||
path.pop()
|
|
||||||
used[i] = 0
|
|
||||||
backtrack(nums, 0)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def backtracking(self, nums, startIndex, used, path, result):
|
||||||
|
result.append(path[:]) # 收集子集
|
||||||
|
for i in range(startIndex, len(nums)):
|
||||||
|
# used[i - 1] == True,说明同一树枝 nums[i - 1] 使用过
|
||||||
|
# used[i - 1] == False,说明同一树层 nums[i - 1] 使用过
|
||||||
|
# 而我们要对同一树层使用过的元素进行跳过
|
||||||
|
if i > 0 and nums[i] == nums[i - 1] and not used[i - 1]:
|
||||||
|
continue
|
||||||
|
path.append(nums[i])
|
||||||
|
used[i] = True
|
||||||
|
self.backtracking(nums, i + 1, used, path, result)
|
||||||
|
used[i] = False
|
||||||
|
path.pop()
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
回溯 利用集合去重
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def subsetsWithDup(self, nums):
|
||||||
|
result = []
|
||||||
|
path = []
|
||||||
|
nums.sort() # 去重需要排序
|
||||||
|
self.backtracking(nums, 0, path, result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def backtracking(self, nums, startIndex, path, result):
|
||||||
|
result.append(path[:]) # 收集子集
|
||||||
|
uset = set()
|
||||||
|
for i in range(startIndex, len(nums)):
|
||||||
|
if nums[i] in uset:
|
||||||
|
continue
|
||||||
|
uset.add(nums[i])
|
||||||
|
path.append(nums[i])
|
||||||
|
self.backtracking(nums, i + 1, path, result)
|
||||||
|
path.pop()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
回溯 利用递归的时候下一个startIndex是i+1而不是0去重
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def subsetsWithDup(self, nums):
|
||||||
|
result = []
|
||||||
|
path = []
|
||||||
|
nums.sort() # 去重需要排序
|
||||||
|
self.backtracking(nums, 0, path, result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def backtracking(self, nums, startIndex, path, result):
|
||||||
|
result.append(path[:]) # 收集子集
|
||||||
|
for i in range(startIndex, len(nums)):
|
||||||
|
# 而我们要对同一树层使用过的元素进行跳过
|
||||||
|
if i > startIndex and nums[i] == nums[i - 1]:
|
||||||
|
continue
|
||||||
|
path.append(nums[i])
|
||||||
|
self.backtracking(nums, i + 1, path, result)
|
||||||
|
path.pop()
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
|
Reference in New Issue
Block a user