The sword refers to Offer_Programming questions_13

Topic description

Input an array of integers, implement a function to adjust the order of the numbers in the array, so that all odd numbers are in the first half of the array, all even numbers are in the second half of the array, and ensure that odd and odd, even and even numbers are between The relative position remains unchanged.
class Solution {
public:
    void reOrderArray(vector<int> &array) {
        int size = array.size();
        vector<int>vt;
        for(int i = 0; i < size; i++){
            if(array[i]%2==1){
                vt.push_back(array[i]);
            }
        }
        for(int i = 0; i < size; i++){
            if(array[i]%2==0){
                vt.push_back(array[i]);
            }
        }
        array = vt;
    }
};

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324778890&siteId=291194637