From 5801ae752325eddc69508dd3635fadc6b74abf69 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 18:53:28 -0500 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 --- problems/0090.子集II.md | 136 +++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 69 deletions(-) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 1a9f8fda..0df0600e 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -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 -不使用used数组 +回溯 利用used数组去重 ```python class Solution: - def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: - 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]]: + def subsetsWithDup(self, nums): result = [] path = [] - nums.sort() - used = [0] * len(nums) - def backtrack(nums, startIdx): - 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) + used = [False] * len(nums) + nums.sort() # 去重需要排序 + self.backtracking(nums, 0, used, path, 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