189. Rotate Array
Given an integer array rotate the array to the right by k steps, where k is non-negative.
!!! attention There are at least three different ways to solve this problem. Try to do with space complexity.
Approach
- Reverse the all the elements.
- Reverse the first elements.
- Reverse the remaining elements.
Complexity
- Time complexity :
- We iterate the whole array only once
- Space complexity :
- No extra space is needed
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
- If we double the array (e.g
[1,2,3]will become[1,2,3,1,2,3])- Rotating array simply means start reading the array after skipping certain step.
Approach
- First create a new array double the size and copy the complete array twice onto it.
- Amount of rotation can be simplified by
- This is equivalent to
- Now rotating to right means start reading elements from index
Complexity
- Time complexity :
- We iterate the array twice only
- Space complexity :
- We create a new array double the size.
Code
public void rotate(int[] nums, int k) {
int [] numsDoubled = new int[2*nums.length];
for(int i = 0; i < nums.length; ++i) {
numsDoubled[i] = nums[i];
numsDoubled[nums.length+i] = nums[i];
}
k = k % nums.length;
for(int i = 0; i < nums.length; ++i) {
nums[i] = numsDoubled[ (nums.length - k) + i];
}
}