-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutationsii.php
More file actions
55 lines (30 loc) · 937 Bytes
/
permutationsii.php
File metadata and controls
55 lines (30 loc) · 937 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
51
52
53
54
55
<?php
class Solution {
public $result = [];
public $vis = [];
/**
* 给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
* @param Integer[] $nums
* @return Integer[][]
*/
function permuteUnique($nums) {
sort($nums);
$this->backTrack($nums, 0, []);
return $this->result;
}
function backTrack($nums, $step, $path) {
if($step === count($nums)){
$this->result[] = $path;
return;
}
for ($i = 0; $i < count($nums); $i++){
if($this->vis[$i] || ($i > 0 && $nums[$i] === $nums[$i - 1] && !$this->vis[$i - 1]))
continue;
$path[] = $nums[$i];
$this->vis[$i] = true;
$this->backTrack($nums, $step + 1, $path);
$this->vis[$i] = false;
array_pop($path);
}
}
}