-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsets.php
More file actions
50 lines (28 loc) · 861 Bytes
/
subsets.php
File metadata and controls
50 lines (28 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
class Solution
{
private $result = [];
/**
* 给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
* 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
* @param Integer[] $nums
* @return Integer[][]
*/
function subsets($nums) {
$this->backTrack($nums, 0, []);
return $this->result;
}
function backTrack($nums, $step, $path)
{
if($step === count($nums)){
$this->result[] = $path;
return;
}
$this->backTrack($nums, $step + 1, $path);
$path[] = $nums[$step];
$this->backTrack($nums, $step + 1, $path);
unset($path[count($path) - 1]);
}
}
$model = new Solution();
var_dump($model->subsets([1, 2, 3]));