diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index ea1eb4cd..9f935b60 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -261,7 +261,9 @@ class Solution: self.path.pop() ``` -### Python3 +### Python3 + +不使用used数组 ```python3 class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: @@ -288,6 +290,28 @@ class Solution: return res ``` +使用used数组 +```python3 +class Solution: + def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: + 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) + return result +``` + ### Go ```Go @@ -526,7 +550,7 @@ func subsetsWithDup(_ nums: [Int]) -> [[Int]] { ### Scala -不使用userd数组: +不使用used数组: ```scala object Solution {