From 26e81746873cb06bd5edca773e55ad6489999210 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Fri, 9 Dec 2022 16:52:22 +0800 Subject: [PATCH 1/4] =?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] + } } ``` From f132a28dba62f3a4573ac43427bcc35c916622f3 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Fri, 9 Dec 2022 17:43:10 +0800 Subject: [PATCH 2/4] =?UTF-8?q?update=200491.=E9=80=92=E5=A2=9E=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97=EF=BC=9A=E4=BF=AE=E6=94=B9=E9=94=99=E5=AD=97?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=20go=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0491.递增子序列.md | 43 +++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index e33a049d..c0196973 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -37,7 +37,7 @@ 在[90.子集II](https://programmercarl.com/0090.子集II.html)中我们是通过排序,再加一个标记数组来达到去重的目的。 -而本题求自增子序列,是不能对原数组经行排序的,排完序的数组都是自增子序列了。 +而本题求自增子序列,是不能对原数组进行排序的,排完序的数组都是自增子序列了。 **所以不能使用之前的去重逻辑!** @@ -78,7 +78,7 @@ if (path.size() > 1) { * 单层搜索逻辑 ![491. 递增子序列1](https://img-blog.csdnimg.cn/20201124200229824.png) -在图中可以看出,**同一父节点下的同层上使用过的元素就不能在使用了** +在图中可以看出,**同一父节点下的同层上使用过的元素就不能再使用了** 那么单层搜索代码如下: @@ -340,30 +340,33 @@ class Solution: ``` ### Go -```golang +```go +var ( + res [][]int + path []int +) func findSubsequences(nums []int) [][]int { - var subRes []int - var res [][]int - backTring(0,nums,subRes,&res) + res, path = make([][]int, 0), make([]int, 0, len(nums)) + dfs(nums, 0) return res } -func backTring(startIndex int,nums,subRes []int,res *[][]int){ - if len(subRes)>1{ - tmp:=make([]int,len(subRes)) - copy(tmp,subRes) - *res=append(*res,tmp) +func dfs(nums []int, start int) { + if len(path) >= 2 { + tmp := make([]int, len(path)) + copy(tmp, path) + res = append(res, tmp) } - history:=[201]int{}//记录本层元素使用记录 - for i:=startIndex;i0&&nums[i]= path[len(path)-1] { + path = append(path, nums[i]) + used[nums[i]] = true + dfs(nums, i+1) + path = path[:len(path)-1] + } } } ``` From 41007ef26c6bfc2f4695c6cd56b8012752fac844 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Fri, 9 Dec 2022 21:21:36 +0800 Subject: [PATCH 3/4] =?UTF-8?q?update=200047.=E5=85=A8=E6=8E=92=E5=88=97II?= =?UTF-8?q?=EF=BC=9A=E6=9B=BF=E6=8D=A2=20go=20=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E6=94=B9=E9=94=99=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0047.全排列II.md | 56 +++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index c809c62d..d9fe8f35 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -49,7 +49,7 @@ **一般来说:组合问题和排列问题是在树形结构的叶子节点上收集结果,而子集问题就是取树上所有节点的结果**。 -在[46.全排列](https://programmercarl.com/0046.全排列.html)中已经详解讲解了排列问题的写法,在[40.组合总和II](https://programmercarl.com/0040.组合总和II.html) 、[90.子集II](https://programmercarl.com/0090.子集II.html)中详细讲解的去重的写法,所以这次我就不用回溯三部曲分析了,直接给出代码,如下: +在[46.全排列](https://programmercarl.com/0046.全排列.html)中已经详细讲解了排列问题的写法,在[40.组合总和II](https://programmercarl.com/0040.组合总和II.html) 、[90.子集II](https://programmercarl.com/0090.子集II.html)中详细讲解了去重的写法,所以这次我就不用回溯三部曲分析了,直接给出代码,如下: ## C++代码 @@ -225,33 +225,37 @@ class Solution: ### Go ```go -var res [][]int -func permute(nums []int) [][]int { - res = [][]int{} - backTrack(nums,len(nums),[]int{}) - return res +var ( + res [][]int + path []int + st []bool // state的缩写 +) +func permuteUnique(nums []int) [][]int { + res, path = make([][]int, 0), make([]int, 0, len(nums)) + st = make([]bool, len(nums)) + sort.Ints(nums) + dfs(nums, 0) + return res } -func backTrack(nums []int,numsLen int,path []int) { - if len(nums)==0{ - p:=make([]int,len(path)) - copy(p,path) - res = append(res,p) - } - used := [21]int{}//跟前一题唯一的区别,同一层不使用重复的数。关于used的思想carl在递增子序列那一题中提到过 - for i:=0;i Date: Fri, 9 Dec 2022 21:23:03 +0800 Subject: [PATCH 4/4] =?UTF-8?q?update=200046.=E5=85=A8=E6=8E=92=E5=88=97:?= =?UTF-8?q?=20=E6=9B=BF=E6=8D=A2=20go=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0046.全排列.md | 45 +++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 805bc457..c437f7ca 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -275,29 +275,34 @@ class Solution: ### Go ```Go -var res [][]int +var ( + res [][]int + path []int + st []bool // state的缩写 +) func permute(nums []int) [][]int { - res = [][]int{} - backTrack(nums,len(nums),[]int{}) - return res -} -func backTrack(nums []int,numsLen int,path []int) { - if len(nums)==0{ - p:=make([]int,len(path)) - copy(p,path) - res = append(res,p) - } - for i:=0;i