diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 9f935b60..57cfd09d 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -261,10 +261,10 @@ class Solution: self.path.pop() ``` -### Python3 +#### Python3 不使用used数组 -```python3 +```python class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: res = [] @@ -291,7 +291,7 @@ class Solution: ``` 使用used数组 -```python3 +```python class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: result = [] @@ -315,26 +315,30 @@ class Solution: ### Go ```Go -var res[][]int -func subsetsWithDup(nums []int)[][]int { - res=make([][]int,0) - sort.Ints(nums) - dfs([]int{},nums,0) - return res +var ( + path []int + res [][]int +) +func subsetsWithDup(nums []int) [][]int { + path, res = make([]int, 0, len(nums)), make([][]int, 0) + sort.Ints(nums) + dfs(nums, 0) + return res } -func dfs(temp, num []int, start int) { - tmp:=make([]int,len(temp)) - copy(tmp,temp) - res=append(res,tmp) - for i:=start;istart&&num[i]==num[i-1]{ - continue - } - temp=append(temp,num[i]) - dfs(temp,num,i+1) - temp=temp[:len(temp)-1] - } +func dfs(nums []int, start int) { + tmp := make([]int, len(path)) + copy(tmp, path) + res = append(res, tmp) + + for i := start; i < len(nums); i++ { + if i != start && nums[i] == nums[i-1] { + continue + } + path = append(path, nums[i]) + dfs(nums, i+1) + path = path[:len(path)-1] + } } ```