mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
@ -207,20 +207,30 @@ class Solution {
|
|||||||
Python:
|
Python:
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
|
def __init__(self):
|
||||||
|
self.paths = []
|
||||||
|
self.path = []
|
||||||
|
|
||||||
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
|
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
|
||||||
res = [] #存放符合条件结果的集合
|
nums.sort()
|
||||||
path = [] #用来存放符合条件结果
|
self.backtracking(nums, 0)
|
||||||
def backtrack(nums,startIndex):
|
return self.paths
|
||||||
res.append(path[:])
|
|
||||||
for i in range(startIndex,len(nums)):
|
def backtracking(self, nums: List[int], start_index: int) -> None:
|
||||||
if i > startIndex and nums[i] == nums[i - 1]: #我们要对同一树层使用过的元素进行跳过
|
# ps.空集合仍符合要求
|
||||||
continue
|
self.paths.append(self.path[:])
|
||||||
path.append(nums[i])
|
# Base Case
|
||||||
backtrack(nums,i+1) #递归
|
if start_index == len(nums):
|
||||||
path.pop() #回溯
|
return
|
||||||
nums = sorted(nums) #去重需要排序
|
|
||||||
backtrack(nums,0)
|
# 单层递归逻辑
|
||||||
return res
|
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()
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user