Leetcode Golang 78. Subsets.go

2019-04-12 11:05:25 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412871

思路

dfs方法,注意中间结果一定要copy到新的变量中,不然会得到空值

code

代码语言:javascript复制
var ans [][]int

func subsets(nums []int) [][]int {
	ans = [][]int{}
	cur := []int{}
	for i := 0; i <= len(nums); i   {
		dfs(nums, i, 0, cur)
	}
	return ans
}
func dfs(nums []int, n, s int, cur []int) {
	if len(cur) == n {
		tmp := make([]int, n)
		copy(tmp, cur)
		ans = append(ans, tmp)
		return
	}
	for i := s; i < len(nums); i   {
		cur = append(cur, nums[i])
		dfs(nums, n, i 1, cur)
		cur = cur[:len(cur)-1]
	}
}

0 人点赞