原题: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]
.
class Solution {
public:void Revise(int nums[],int s,int e){
int tmp;
for(;s<e;s++,e--){
tmp=nums[e];
nums[e]=nums[s];
nums[s]=tmp;
}
}
void rotate(int nums[], int n, int k) {
k%=n;
Revise(nums,0,n-1);
Revise(nums,k,n-1);
Revise(nums,0,k-1);
}
};