反转前半部分,在反转后半部分,最后整个反转。
void rotate(vector<int>& nums, int k) {
int len = nums.size();
k = k % len;
reverse(nums,0,len-k-1);
reverse(nums,len-k,len-1);
reverse(nums,0,len-1);
}
void reverse(vector<int>& nums,int head,int tail){
while(head<tail){
int tmp = nums[head];
nums[head++] = nums[tail];
nums[tail--] = tmp;
}
}