forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.php
35 lines (32 loc) · 804 Bytes
/
Solution.php
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
class Solution {
/**
* @param integer[] $nums
* @return void
*/
function nextPermutation(&$nums) {
$n = count($nums);
$i = $n - 2;
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) {
$i--;
}
if ($i >= 0) {
$j = $n - 1;
while ($j >= $i && $nums[$j] <= $nums[$i]) {
$j--;
}
$temp = $nums[$i];
$nums[$i] = $nums[$j];
$nums[$j] = $temp;
}
$this->reverse($nums, $i + 1, $n - 1);
}
function reverse(&$nums, $start, $end) {
while ($start < $end) {
$temp = $nums[$start];
$nums[$start] = $nums[$end];
$nums[$end] = $temp;
$start++;
$end--;
}
}
}