File tree Expand file tree Collapse file tree 1 file changed +23
-13
lines changed Expand file tree Collapse file tree 1 file changed +23
-13
lines changed Original file line number Diff line number Diff line change @@ -207,20 +207,30 @@ class Solution {
207
207
Python:
208
208
``` python3
209
209
class Solution :
210
+ def __init__ (self ):
211
+ self .paths = []
212
+ self .path = []
213
+
210
214
def subsetsWithDup (self , nums : List[int ]) -> List[List[int ]]:
211
- res = [] # 存放符合条件结果的集合
212
- path = [] # 用来存放符合条件结果
213
- def backtrack (nums ,startIndex ):
214
- res.append(path[:])
215
- for i in range (startIndex,len (nums)):
216
- if i > startIndex and nums[i] == nums[i - 1 ]: # 我们要对同一树层使用过的元素进行跳过
217
- continue
218
- path.append(nums[i])
219
- backtrack(nums,i+ 1 ) # 递归
220
- path.pop() # 回溯
221
- nums = sorted (nums) # 去重需要排序
222
- backtrack(nums,0 )
223
- return res
215
+ nums.sort()
216
+ self .backtracking(nums, 0 )
217
+ return self .paths
218
+
219
+ def backtracking (self , nums : List[int ], start_index : int ) -> None :
220
+ # ps.空集合仍符合要求
221
+ self .paths.append(self .path[:])
222
+ # Base Case
223
+ if start_index == len (nums):
224
+ return
225
+
226
+ # 单层递归逻辑
227
+ for i in range (start_index, len (nums)):
228
+ if i > start_index and nums[i] == nums[i- 1 ]:
229
+ # 当前后元素值相同时,跳入下一个循环,去重
230
+ continue
231
+ self .path.append(nums[i])
232
+ self .backtracking(nums, i+ 1 )
233
+ self .path.pop()
224
234
```
225
235
226
236
Go:
You can’t perform that action at this time.
0 commit comments