题目链接
解题思路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| func copySlice(src []int) []int { temp := []int{} for _, i := range src { temp = append(temp, i) } return temp }
func subsets(nums []int) [][]int { result := [][]int{} result = append(result, []int{}) for i := 0; i < len(nums); i++ { length := len(result) for j := 0; j < length; j++ { result = append(result, append(copySlice(result[j]), nums[i])) } } return result }
|
注意
代码直接写成for j := 0; j < length; j++
会超出内存限制,我在本地虚拟机上运行时,会卡爆电脑,原因正在找。