/** * @param {number[]} nums * @param {number} k * @return {void} Do not return anything, modify nums in-place instead. */ var rotate = function (nums, k) { const len = nums.length; let count = len - 1;
while (count >= len - k) { nums.unshift(nums.pop()); count--; } };
方法二:
1 2 3 4 5 6 7 8 9 10
/** * @param {number[]} nums * @param {number} k * @return {void} Do not return anything, modify nums in-place instead. */ var rotate = function (nums, k) { const len = nums.length; const temp = nums.splice(len - k, k); nums.unshift(...temp); };