class Solution { public: void reOrderArray(vector<int> &array) { int length = array.size(); int count = 0; queue<int> even; for (int i = 0; i != length; ++i){ if (array[i]%2) array[i - count] = array[i]; else{ even.push(array[i]); ++count; } } for (int i = length - count; i != length; ++i){ array[i] = even.front(); even.pop(); } } };
很low的一个方法,遍历整个向量,遇到偶数压入队列,遇到奇数往前移动
1、count是一个重要计数量,记录当前遇到的偶数个数,在奇数移动和后边偶数插入都有用
欢迎交流指正!!!