Skip to content

Commit ecb99c2

Browse files
authored
Update 0090.子集II.md
修正python代码补充注释
1 parent e447076 commit ecb99c2

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

problems/0090.子集II.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,30 @@ class Solution {
207207
Python:
208208
```python3
209209
class Solution:
210+
def __init__(self):
211+
self.paths = []
212+
self.path = []
213+
210214
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()
224234
```
225235

226236
Go:

0 commit comments

Comments
 (0)