leetcode 78. 子集

题解

倒序遍历数组nums,依次添加到前边的子集中

示例代码(go)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func subsets(nums []int) [][]int {
res := make([][]int, 0)
res = append(res, []int{})
for i := len(nums)-1; i >= 0; i-- {
length := len(res)
for j := 0; j < length; j++ {
tmp := make([]int, 0)
tmp = append(tmp, nums[i])
tmp = append(tmp, res[j]...)
res = append(res, tmp)
}
}
return res
}