题解 | #调整数组顺序使奇数位于偶数前面(一)# O(N2) O(1)解法
调整数组顺序使奇数位于偶数前面(一)
http://www.nowcoder.com/practice/ef1f53ef31ca408cada5093c8780f44b
class Solution {
public:
vector<int> reOrderArray(vector<int>& array) {
// write code here
int last=0;
int num=1;
int len=array.size();
for (int i=0;i<len;i++)
{
if(array[i]%2!=0)
{
array.insert(array.begin()+last+num, array[i]);
array.erase(array.begin()+i+1);
num++;
}
else
{
if(i==0) {last=i; num=0;}
else if((array[i-1]%2!=0)&&(array[i]%2==0)) {last=i; num=0;}
}
}
return array;
}
};