Open Tcdian opened 3 years ago
/**
Do not return anything, modify nums in-place instead.
*/
function rotate(nums: number[], k: number): void {
k = k % nums.length;
reverse(0, nums.length - 1);
reverse(0, k - 1);
reverse(k, nums.length - 1);
function reverse(start: number, end: number) {
while (start < end) {
[nums[start], nums[end]] = [nums[end], nums[start]];
start++;
end--;
}
}
};
189. Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative.
Follow up
Example 1
Example 2
Constraints