Title Requirements:
Enter an array of integers to implement a function that adjusts the order of the numbers in the array so that all the odd digits are placed in the first half of the array, all the even digits are located in the second half of the array, and the relative positions between the odd and odd, even and even, are guaranteed.
Problem Solving methods:
Method One: Time complexity O (n^2)
It's easiest to think: Scan this array from the beginning, take out this number each time an even number is encountered, and move all the numbers that are behind that number forward. After you have moved, there is an empty space at the end of the array, in which case the even number is placed.
Method Two: Time complexity O (n)
Re-establish a vector, then perform two scans: first scan odd, second scan even, complete, no nonsense, directly on the code:
1 classsolution{2 Public:3 voidReorderarray (vector<int> &Array) {4 intLen =array.size ();5vector<int>Vec (Array.begin (), Array.end ());6 intL =0;7 for(intX:vec) {8 if(%2==1){9array[l++] =x;Ten } One } A for(intY:vec) { - if(y%2==0){ -array[l++] =y; the } - } - } -};
The adjustment of a sword to an offer the array order makes the odd number precede even