https://leetcode.cn/problems/rotate-array/
class Solution {
public void rotate(int[] nums, int k) {
int step = k % nums.length;
int length = nums.length;
reverse(nums, 0, length - 1);
reverse(nums, 0, step - 1);
reverse(nums, step, length -1);
}
private void reverse(int[] nums, int start, int end) {
if (start == end) {
return;
}
for (int i = start; i < (end - start)/2 + start; i++) {
int temp = nums[i];
nums[i] = nums[end - i];
nums[end - i] = temp;
}
}
}
网友评论