-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsetsii.php
More file actions
54 lines (34 loc) · 1.04 KB
/
subsetsii.php
File metadata and controls
54 lines (34 loc) · 1.04 KB
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
51
52
53
54
<?php
class Solution
{
public $result = [];
/**
* 给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
*
* 解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
* @param Integer[] $nums
* @return Integer[][]
*/
function subsetsWithDup($nums)
{
sort($nums);
$this->backTrack($nums, 0, []);
return $this->result;
}
function backTrack($nums, $step, $path, $is_choose_pre = false)
{
if($step === count($nums)){
$this->result[] = $path;
return;
}
$this->backTrack($nums, $step + 1, $path);
if(!$is_choose_pre && $step > 0 && $nums[$step] == $nums[$step - 1]){
return;
}
$path[] = $nums[$step];
$this->backTrack($nums, $step + 1, $path, true);
unset($path[count($path) - 1]);
}
}
$model = new Solution();
var_dump($model->subsetsWithDup([1, 2, 2]));