Title Description
Problem-solving ideas
Odd and odd title requirements, the relative position between the even-even and constant, can not be used similar to the head and tail pointers bis quicksort calculated manner closer to the center. All have two pointers starting from the head, to lock the first odd and even scan, to move the even, odd inserted. Specific steps are as follows:
1. The size of the array is determined whether 0, 0 is returned directly;
2. initialize the pointer i = 0, j = i + 1, and a temporary variable tmp;
3. As long as j satisfying j <size, 4 to 8 to perform operation cycle;
4. From left to right, i started looking for a first position assigned to the even-numbered i;
5. j = i + 1, looking from left to right a first position assigned to the odd-J;
6. If j is greater than the size of the array, the array has fulfilled the request out of the loop;
7. After the even i ~ j-1 position of the shift, the value of the original insertion position j in location i;
8.i point to the next element (i ++), returns to step 4;
C ++ code to achieve:
class Solution { public: void reOrderArray(vector<int> &array) { int size=array.size(); if(size==0){ return; } int i=0,j=i+1, tmp; while(i<size && j<size){ while(array[i]%2!=0){ //i确定偶数位置 i++; } j=i+1; The while (Array [J]% 2 ! = . 1 ) { // J determine odd positions J ++ ; } IF (J> = size) { // J exceeds the length of the array, the end return ; } tmp = Array [J]; for ( int K = J; K> I; - K) { Array [K] = Array [- K- . 1 ]; } Array [I] = tmp; I ++ ; } } };