-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.php
More file actions
47 lines (28 loc) · 822 Bytes
/
permutations.php
File metadata and controls
47 lines (28 loc) · 822 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
<?php
class Solution {
private $result = [];
/**
* 给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
* @param Integer[] $nums
* @return Integer[][]
*/
function permute($nums) {
$this->backTrack($nums, 0, []);
return $this->result;
}
function backTrack($nums, $step, $path) {
if($step == count($nums)){
$arr = $path;
$this->result[] = $arr;
return;
}
for ($i = 0; $i < count($nums); $i++){
if(in_array($nums[$i], $path)){
continue;
}
$path[] = $nums[$i];
$this->backTrack($nums, $step + 1, $path);
array_pop($path);
}
}
}