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].
Congratulation! My runtime beats 98.27% of java submissions! It is the best one for me by now.
public class Solution {
public void rotate(int[] nums, int k) {
int t = k / nums.length +3;
int[] result = new int[nums.length*t];
for(int i = 0; i < t ;i++){
System.arraycopy(nums,0,result,i*nums.length,nums.length);
}
System.arraycopy(result,2*nums.length-k,nums,0,nums.length);
}
}