|
| 1 | +class Solution { |
| 2 | + |
| 3 | + /** |
| 4 | + * @param Integer[] $nums |
| 5 | + * @return Integer[][] |
| 6 | + */ |
| 7 | + function permute($nums) { |
| 8 | + $res = []; |
| 9 | + if ($nums == null || count($nums) == 0) return $res; |
| 10 | + $currentX = []; |
| 11 | + $used = array_fill(0, count($nums), false); |
| 12 | + for($j=0; $j<count($used);$j++) |
| 13 | + { |
| 14 | + $used[$j] = false; |
| 15 | + } |
| 16 | + self::bt($currentX, $nums, $used, $res); |
| 17 | + return $res; |
| 18 | + } |
| 19 | + |
| 20 | + function bt($currentX, &$nums, $used, &$res) |
| 21 | + { |
| 22 | + if (count($currentX) == count($nums)) |
| 23 | + { |
| 24 | + array_push($res, $currentX); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + for ($i = 0; $i < count($nums); $i++) |
| 29 | + { |
| 30 | + |
| 31 | + if ($used[$i] == true) continue; |
| 32 | + array_push($currentX, $nums[$i]); |
| 33 | + $used[$i] = true; |
| 34 | + |
| 35 | + self::bt($currentX, $nums, $used, $res); |
| 36 | + |
| 37 | + array_pop($currentX); |
| 38 | + $used[$i] = false; |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments