189. Rotate Array
There are at least three different ways to solve this problem. Try to do with $\text{O}(1)$ space complexity.Approach
Complexity
Code
public void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}Another Approach
Intuition
Approach
Complexity
Code
Last updated