From 26e81746873cb06bd5edca773e55ad6489999210 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Fri, 9 Dec 2022 16:52:22 +0800 Subject: [PATCH] =?UTF-8?q?update=200090.=E5=AD=90=E9=9B=86II=EF=BC=9A?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=20go=20=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20python=20=E7=9A=84markdown=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0090.子集II.md | 46 +++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 21 deletions(-) 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] + } } ```