go 回溯实现
package main import "sort" /** * * @param A int整型一维数组 * @return int整型二维数组 */ func subsets( A []int ) [][]int { // write code here if len(A) == 0 { return nil } sort.Ints(A) ret := make([][]int, 0) path := make([]int, 0) // 兵役闭包函数 var do func(start int, path []int) do = func(start int, path []int) { // 把path追加到结果中 tmp := make([]int, len(path)) copy(tmp, path) ret = append(ret, tmp) // 起始位置大于A长度,则退出 if start >= len(A) { return } // 从起始位置开始 for i:= start; i<len(A); i++{ path = append(path, A[i]) do(i+1, path) path = path[:len(path)-1] } } do(0, path) return ret }