题目描述
输入一个无序整数数组,调整数组中数字的顺序, 所有偶数位于数组的前半部分,使得所有奇数位于数组的后半部分。
要求时间复杂度为O(n)。
#include <iostream>
#include <vector>
std::vector<int> evenodd(std::vector<int> array){
int evenIndex = 0; int oddIndex = array.size()-1;
while(evenIndex < oddIndex){
if (array[evenIndex]%2 != 0 && array[oddIndex]%2 == 0){
int temp = array[evenIndex];
array[evenIndex] = array[oddIndex];
array[oddIndex] = temp;
evenIndex++; oddIndex--;
}
while(array[evenIndex]%2 == 0) evenIndex++;
while(array[oddIndex]%2 != 0) oddIndex--;
}
return array;
}
int main(int argc, char* argv[]){
std::vector<int> vec{1,1,4,8,6,2,2};
std::vector<int> rtn = evenodd(vec);
for (auto i : rtn) std::cout << i << " ";
std::cout << std::endl;
return 0;
}
为什么 没有通过所有测例??