From 74ccecbdcd80d7c2ca9107e73ff65d5a35e352d7 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 8 Dec 2022 21:30:41 +0800 Subject: [PATCH] =?UTF-8?q?update=200078.=E5=AD=90=E9=9B=86:=20=E6=9B=BF?= =?UTF-8?q?=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/0078.子集.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/problems/0078.子集.md b/problems/0078.子集.md index e271a96f..b5958e65 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -227,25 +227,25 @@ class Solution: ## Go ```Go -var res [][]int -func subset(nums []int) [][]int { - res = make([][]int, 0) - sort.Ints(nums) - Dfs([]int{}, nums, 0) - return res +var ( + path []int + res [][]int +) +func subsets(nums []int) [][]int { + res, path = make([][]int, 0), make([]int, 0, len(nums)) + dfs(nums, 0) + return res } -func Dfs(temp, nums []int, start int){ - tmp := make([]int, len(temp)) - copy(tmp, temp) - res = append(res, tmp) - for i := start; i < len(nums); i++{ - //if i>start&&nums[i]==nums[i-1]{ - // continue - //} - temp = append(temp, nums[i]) - Dfs(temp, nums, 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++ { + path = append(path, nums[i]) + dfs(nums, i+1) + path = path[:len(path)-1] + } } ```