From 1ae78a86ca73cf229acbbfe8fa6d16a505814e34 Mon Sep 17 00:00:00 2001 From: hitorig <86967184+hitorig@users.noreply.github.com> Date: Fri, 6 Aug 2021 17:43:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200046.=E5=85=A8=E6=8E=92=E5=88=97.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0046.全排列.md | 39 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 3ee7271e..3b3ba180 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -227,24 +227,27 @@ class Solution: Go: ```Go -var result [][]int -func backtrack(nums,pathNums []int,used []bool){ - if len(nums)==len(pathNums){ - tmp:=make([]int,len(nums)) - copy(tmp,pathNums) - result=append(result,tmp) - //result=append(result,pathNums) - return - } - for i:=0;i Date: Fri, 6 Aug 2021 18:03:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200047.=E5=85=A8=E6=8E=92=E5=88=97II?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0047.全排列II.md | 48 +++++++++++++++++------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index 66107c95..c8764241 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -228,35 +228,31 @@ Go: ```go var res [][]int func permute(nums []int) [][]int { - res = [][]int{} - sort.Ints(nums) - dfs(nums, make([]int, 0), make([]bool, len(nums))) - return res + 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) + } + used := [21]int{}//跟前一题唯一的区别,同一层不使用重复的数。关于used的思想carl在递增子序列那一题中提到过 + for i:=0;i