-
Notifications
You must be signed in to change notification settings - Fork 0
/
189.php
49 lines (40 loc) · 863 Bytes
/
189.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
class Solution {
/**
* @param Integer[] $nums
* @param Integer $k
* @return NULL
*/
function rotate(&$nums, $k)
{
if ($k == 0) return;
$len = count($nums);
$k %= $len;
$this->reverse($nums, 0, $len - 1);
$this->reverse($nums, 0, $k - 1);
$this->reverse($nums, $k, $len - 1);
return;
}
/**
* revert an array
*
* @param $nums
* @param $start
* @param $end
*/
private function reverse(&$nums, $start, $end)
{
while ($start < $end) {
$tmp = $nums[$start];
$nums[$start] = $nums[$end];
$nums[$end] = $tmp;
$start ++;
$end --;
}
return;
}
}
$nums = [1,2,3,4,5,6,7];
$sol = new Solution();
$sol->rotate2($nums, 3);
var_export($nums);