由买买提看人间百态

topics

全部话题 - 话题: halflen
(共0页)
r*****e
发帖数: 792
1
来自主题: JobHunting版 - 问一道G家经典老题
void arrayShuffle(vector &v, int first, int last) {
int mid, halfLen=(last-first)/2;
if (last-first<=2)//already in place
return;
if (last-first==4) {//swap middle two
swap(v[first+1], v[first+2]);
return;
}
/* regular cases */
mid = halfLen/2+first;
vector::iterator it = v.begin();
reverse(it+mid, it+mid+halfLen);//reverse entire mid section first
reverse(it+mid, it+mid+(mid-first));
reverse(it+mid+(mid-first), it+mid+halfLen);
a... 阅读全帖
(共0页)