> 子集

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

输入:nums = [1,2,3] 输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

输入:nums = [0] 输出:[[],[0]]

AC 代码

func subsets(nums []int) [][]int {

    l := list.New()
    result := list.New()
    
    for i := 0; i <= len(nums); i++ {
         dfs(nums, 0, i, l, result)
    }
   
    arr := make([][]int, result.Len())
    k := 0
    for e := result.Front(); e != nil; e = e.Next(){
        curl := e.Value.(*list.List)
        arr[k] = make([]int, curl.Len())
        k++
    }

    i := 0;
    for e := result.Front(); e != nil; e = e.Next() {
        //fmt.Println(e.Value.(*list.List).Front().Value)
        curl := e.Value.(*list.List)
        j := 0
        for p := curl.Front(); p != nil; p = p.Next() {
           //fmt.Println(p.Value)
           arr[i][j] = p.Value.(int)
           j++
        }
        
        i++;
    }

    return arr
}

func dfs(nums []int, start int, len int, l *list.List, result *list.List) {

    if start == len {
        a := list.New()
        for e := l.Front(); e != nil; e = e.Next() {
            fmt.Println(e.Value.(int))
            a.PushBack(e.Value)
        }
        result.PushBack(a)
        return
    }

    for i := start; i < len; i++ {
        l.PushBack(nums[i])
        dfs(nums, i+1, len, l, result)
        b := l.Back()
        l.Remove(b)
    }
}

https://mp.weixin.qq.com/s/81TnI22hNoBbf9xKo-ZJLQ