From f132a28dba62f3a4573ac43427bcc35c916622f3 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Fri, 9 Dec 2022 17:43:10 +0800 Subject: [PATCH] =?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] + } } } ```