The title description enters an array of integers, implements a function to adjust 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.
1 classSolution {2 Public:3 voidReorderarray (vector<int> &Array) {4 if(array.size () = =0)5 return;6 inti =0, j = array.size ()-1;7vector<int>v1, v2;8 9 for(inti =0; I < array.size (); i++){Ten if(Array[i]%2!=0) One V1.push_back (Array[i]); A Else - V2.push_back (Array[i]); - } the intK =0; - for(intj =0; J < V1.size (); J + +){ -ARRAY[K] =V1[j]; -k++; + } - for(intj =0; J < V2.size (); J + +){ +ARRAY[K] =V2[j]; Ak++; at } - } -};
1 classSolution {2 Public:3 voidReorderarray (vector<int> &Array) {4 if(array.size () = =0)5 return;6 inti =0, j = array.size ()-1;7 while(J >i) {8 if(Array[i]%2!=0)9i++;Ten if(Array[j]%2==0) Onej--; A if(Array[i]%2==0&& Array[j]%2!=0){ - inttemp =Array[i]; -Array[i] =Array[j]; theARRAY[J] =temp; - } - } - } +};
Interview 14 Adjust the array order so that the odd digits are preceded by even numbers