189. Rotate Array
requirement: rotate an array of n elements to the right by k steps.
for example, with n=7 and k=3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]
note: try to come up as many solutions as you can, there are at least 3 different ways to solve this problme.
strategy 1:
using arraycopy(Object src, int srcPos, Object dest, int destPos, int length) from the Java APIlibrary
1. first create a new array called newList, let k= length, n = src.length
2. copy the subarray in src from srcPos(n-k) to srcPos(n-k) + k-1 substring into the new array at the position from dest(0) to dest(0) + k -1
3. then copy the subarray in src from srcPos(0) to srcPos(k)+n-k-1 substring into the new array position from dest(k) to dest(k) + n-k -1
4. then copy the whole new string into the original string
public class Solution{
public void rotate(int[] nums, int k){
int n = nums.length;
int[] newList = new int[n];
if (n > 1){
k = k % n;
System.arraycopy(nums, n-k, newList, 0, k);
System.arraycopy(nums, 0, newList, k, n-k);
System.arraycopy(newList, 0, nums, 0, n);
}
}
}
strategy 2:
1. first reverse the substring (0, n-k-1)
2. then reverse the substring (n-k, n-1)
3. finally reverse the whole string and return the new string
total space O(1)
public class Solution{
public void rotate(int[] nums, int k){
if(nums == null || nums.length < 2){
return;
}
k = k % nums.length;
reverse(nums, 0, nums.length-k-1);
reverse(nums, nums.length-k, nums.length-1);
reverse(nums, 0, nums.length-1);
}
private void reverse(int[] nums, int i, int j){
int tmp = 0;
while(i<j){
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
i++;
j--;
}
}
}
strategy 3
1. using stack to store the substring (n-k, n-1)
2. then replace array nums[i+k] = nums[i] from n-k-1 to 0
3. then assign substring (0, k-1) with the stack element using pop() function
public class Solution{
public void rotate(int[] nums, int k){
if(nums == null || nums.length < 2){
return;
}
k = k % nums.length;
Stack<Integer> stack = new Stack<Integer>();
for(int i= nums.length-1; i>= nums.length - k; i--){
stack.push(nums[i]);
}
for(int i= nums.length-k-1; i>=0; i--){
nums[i+k]=nums[i];
}
for(int i=0; i<k; i++){
nums[i]=stack.pop();
}
}
}
requirement: rotate an array of n elements to the right by k steps.
for example, with n=7 and k=3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]
note: try to come up as many solutions as you can, there are at least 3 different ways to solve this problme.
strategy 1:
using arraycopy(Object src, int srcPos, Object dest, int destPos, int length) from the Java APIlibrary
1. first create a new array called newList, let k= length, n = src.length
2. copy the subarray in src from srcPos(n-k) to srcPos(n-k) + k-1 substring into the new array at the position from dest(0) to dest(0) + k -1
3. then copy the subarray in src from srcPos(0) to srcPos(k)+n-k-1 substring into the new array position from dest(k) to dest(k) + n-k -1
4. then copy the whole new string into the original string
public class Solution{
public void rotate(int[] nums, int k){
int n = nums.length;
int[] newList = new int[n];
if (n > 1){
k = k % n;
System.arraycopy(nums, n-k, newList, 0, k);
System.arraycopy(nums, 0, newList, k, n-k);
System.arraycopy(newList, 0, nums, 0, n);
}
}
}
strategy 2:
1. first reverse the substring (0, n-k-1)
2. then reverse the substring (n-k, n-1)
3. finally reverse the whole string and return the new string
total space O(1)
public class Solution{
public void rotate(int[] nums, int k){
if(nums == null || nums.length < 2){
return;
}
k = k % nums.length;
reverse(nums, 0, nums.length-k-1);
reverse(nums, nums.length-k, nums.length-1);
reverse(nums, 0, nums.length-1);
}
private void reverse(int[] nums, int i, int j){
int tmp = 0;
while(i<j){
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
i++;
j--;
}
}
}
strategy 3
1. using stack to store the substring (n-k, n-1)
2. then replace array nums[i+k] = nums[i] from n-k-1 to 0
3. then assign substring (0, k-1) with the stack element using pop() function
public class Solution{
public void rotate(int[] nums, int k){
if(nums == null || nums.length < 2){
return;
}
k = k % nums.length;
Stack<Integer> stack = new Stack<Integer>();
for(int i= nums.length-1; i>= nums.length - k; i--){
stack.push(nums[i]);
}
for(int i= nums.length-k-1; i>=0; i--){
nums[i+k]=nums[i];
}
for(int i=0; i<k; i++){
nums[i]=stack.pop();
}
}
}