[Prove safety offer apex] - the order of the array so that the adjustment in front of even-odd

Time limit: C / C ++ 1 second, other languages 2 seconds space limitations: C / C ++ 32M, other languages 64M heat index: 738 995
this question knowledge: arrays
of Algorithm video explaining
subject description
enter an array of integers, to realize a function to adjust the an array of numbers in sequence, such that all the odd part of the front half of the array, is located in the second half of all the even array, and to ensure that the relative position between the odd and odd-even and even-numbered unchanged.
Links: adjust the order of the array so that the even-odd in front .
Solving ideas
front odd looking back, looking forward and backward to find the even and exchanged. Such exchange ensures the relative position between the odd and odd-even and even-numbered unchanged.
Code

class Solution {
public:
    void reOrderArray(vector<int> &array) {
    if (array.size() == 0 || array.size() < 2) {
            return;
        }
        int n = array.size();
        for (int i = 1; i < n; i++) {
            // 当前元素是奇数,就移动到奇数序列
            if (array[i] % 2 != 0) {
                int value = array[i];
                int cur = i;
                while (cur > 0 && (array[cur - 1] % 2 == 0)) {
                    array[cur] = array[cur - 1];
                    cur--;
                }
                array[cur] = value;
            }
            // 当前元素是偶数,无须移动
        }
    }
};
Published 57 original articles · won praise 356 · views 20000 +

Guess you like

Origin blog.csdn.net/famur/article/details/105364396