78. 子集

链接 (opens new window)

20240215131937_image.png

20240215132005_image.png

class Solution {

    public $result;

    /**
     * @param Integer[] $nums
     * @return Integer[][]
     */
    function subsets($nums) {
        $this->dfs($nums,0,[]);
        return $this->result;
    }

    function dfs($nums, $start, $path) {
        $this->result[] = $path;
        for ($i = $start, $iMax = count($nums); $i < $iMax; $i++) {
            $path[] = $nums[$i];
            $this->dfs($nums, $i + 1, $path);
            array_pop($path);
        }
    }
}

copy success
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23