题解 | #旋转数组#
旋转数组
https://www.nowcoder.com/practice/e19927a8fd5d477794dac67096862042
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 旋转数组
* @param n int整型 数组长度
* @param m int整型 右移距离
* @param a int整型一维数组 给定数组
* @return int整型一维数组
*/
function solve( n , m , a ) {
// write code here
const mo = m % n;
const s = a.splice(n-mo,mo);
a.unshift(...s);
return a;
}
module.exports = {
solve : solve
};
需要考虑移动次数大于数组长度的情况,所以进行求模运算;
所以需要做的就是将数组最后 模(int) 位的整数移动到数组前端;
暴力解:切割数组再入队。
#刷题coding#