提交 7c0d0358 编写于 作者: 锅巴胸's avatar 锅巴胸

feature: 旋转数组指定位置

php/js
上级 b99eff18
class Solution {
static rotate(nums, k) {
let i = 0;
while (i < k) {
let pop = nums.pop();
nums.unshift(pop);
i++;
}
console.log(nums);
}
}
let nums = [1, 2, 3, 4, 5, 6, 7], k = 3;
Solution.rotate(nums, k);
<?php
class Solution
{
/**
* @param Integer[] $nums
* @param Integer $k
* @return NULL
*/
static function rotate(&$nums, $k)
{
$i = 0;
while ($i < $k) {
$pop = array_pop($nums);
echo "<pre>";
// print_r($nums);
// echo $pop;
array_unshift($nums, $pop);
$i++;
}
print_r($nums);
}
}
$nums = [1, 2, 3, 4, 5, 6, 7];
$k = 3;
Solution::rotate($nums, $k);
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册