while(k>=n){k=k-n;}std::rotate(nums, nums+n-k, nums+n);
......
具体可以看stl_algo.h,有forward, bidirectional, random iterator版本。
我不明白的是forward_iterator版本:
_ForwardIterator __first2 = __middle;do{std::iter_swap(__first, __first2);++__first;++__first2;if (__first == __middle)__middle = __first2;}while (__first2 != __last);__first2 = __middle;while (__first2 != __last){std::iter_swap(__first, __first2);++__first;++__first2;if (__first == __middle)__middle = __first2;else if (__first2 == __last)__first2 = __middle;}
第一个do while loop的作用是什么。
这么写:
void myswap(int& m, int& n)
{m = m^n;n = m^n;m = m^n;
}void rotate(int nums[], int n, int k) {while (k>=n){k = k - n;}if (k <= 0) return;k = n - k;int p = k;int s = 0;int e = n;while (p!=n){myswap(nums[s], nums[p]);s++; p++;if (s==k){k = p;}else if(p==n){p = k;}}
}
提交也过了。