/
189. 轮转数组.php
53 lines (48 loc) · 1.26 KB
/
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
50
51
52
53
<?php
/**
* Created by PhpStorm
* User: jtahstu
* Time: 2022/5/26 16:36
* Des: 189. 轮转数组
* https://leetcode.cn/problems/rotate-array/
* 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
*/
class Solution
{
/**
* @param Integer[] $nums
* @param Integer $k
* @return NULL
*/
function rotate(&$nums, $k)
{
$len = count($nums);
$k = $k % $len;
if ($k == 0) return;
$this->reverse($nums, 0, $len - $k - 1);
$this->reverse($nums, $len - $k, $len - 1);
$this->reverse($nums, 0, $len - 1);
//方法二
$total = count($nums);
if ($total < $k) {
$k = $k % $total;
}
$tmp = array_splice($nums, 0, $total - $k);
$nums = array_merge($nums, $tmp);
}
function reverse(&$nums, $start, $end)
{
while ($start < $end) {
$temp = $nums[$start];
$nums[$start] = $nums[$end];
$nums[$end] = $temp;
$start++;
$end--;
}
}
}
/**
* 执行用时:44 ms, 在所有 PHP 提交中击败了99.53%的用户
* 内存消耗:30 MB, 在所有 PHP 提交中击败了36.75%的用户
* 通过测试用例:38 / 38
*/