题解 | #调整数组顺序使奇数位于偶数前面(一)#
调整数组顺序使奇数位于偶数前面(一)
http://www.nowcoder.com/practice/ef1f53ef31ca408cada5093c8780f44b
直接的方法,O(n^2)的时间复杂度
int* reOrderArray(int* array, int arrayLen, int* returnSize ) {
int temp=0;
int count = 0;
int *left =array;
int *right =array;
int i;
if(arrayLen<=1)
{
*returnSize = arrayLen;
return array;
}
*returnSize = arrayLen;
while(right<array+arrayLen&&left<array+arrayLen)
{
while(left<array+arrayLen&&((*left)%2)) left++;
right= left;
while(right<array+arrayLen&&!((*right)%2)) right++;
if(right>=array+arrayLen||left>=array+arrayLen)
break;
temp = *right;
for(i=right-left;i>=1;i--)
left[i]=left[i-1];
left[0]=temp;
}
return array;
}