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);
}
}
本文介绍了一种方法,用于将数组元素向右移动指定步数,并提供了Java代码实现,效率高,性能优异。
409

被折叠的 条评论
为什么被折叠?



