From ecb99c2a296ba503c4d71e59a256b8de0c77db58 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Mon, 11 Oct 2021 14:44:00 +0800 Subject: [PATCH] =?UTF-8?q?Update=200090.=E5=AD=90=E9=9B=86II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正python代码补充注释 --- problems/0090.子集II.md | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 7ae7c6c2..665137e9 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -207,20 +207,30 @@ class Solution { Python: ```python3 class Solution: + def __init__(self): + self.paths = [] + self.path = [] + def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: - res = [] #存放符合条件结果的集合 - path = [] #用来存放符合条件结果 - def backtrack(nums,startIndex): - res.append(path[:]) - for i in range(startIndex,len(nums)): - if i > startIndex and nums[i] == nums[i - 1]: #我们要对同一树层使用过的元素进行跳过 - continue - path.append(nums[i]) - backtrack(nums,i+1) #递归 - path.pop() #回溯 - nums = sorted(nums) #去重需要排序 - backtrack(nums,0) - return res + 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() ``` Go: