All files / leetCode 0189.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13  4x 2x 2x 2x       2x 2x 2x    
export default function rotate(nums: number[], k: number): number[] {
  if (nums.length < 2 || k === 0)
    return nums
  const rotate = k % nums.length
  const rotatedNums = [
    ...nums.slice(nums.length - rotate),
    ...nums.slice(0, nums.length - rotate),
  ]
  nums.splice(0)
  nums.push(...rotatedNums)
  return nums
}