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]
.
旋转数组,采用的方法主要是,构造一个中间数据,存储旋转后的结果,再将中间数组的结果写回原数组
public class Solution {
public void rotate(int[] nums, int k) {
int l=nums.length;
int w=k%l;
int[] x=new int[l];
for(int i=0;i<l;i++)
x[i]=0;
for(int i=l-w;i<l;i++)
x[i-l+w]=nums[i];
for(int i=0;i<l-w;i++)
x[w+i]=nums[i];
for(int i=0;i<l;i++)
nums[i]=x[i];
}
}