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. Analysis: The problem is added when practicing on OJ, that is, the order of the adjusted numbers is constant. At present, only one method can be thought of, followed by other methods to add. Create a new array, scan the original array two times, and first remove all the odd numbers from the original array and deposit them in the new array. The second time finds all the even numbers in the new array. The code is implemented as follows:
1 Public classSolution {2 Public voidReorderarray (int[] Array) {3 intLength =Array.Length;4 int[] temp =New int[length];5 if(length==1| | length==0| | array==NULL){6 return ;7 }8 intj = 0 ;9 for(inti = 0; i<length; i++){Ten if((array[i]&1) ==1){ OneTEMP[J] =Array[i]; AJ + + ; - } - } the for(inti = 0; i<length; i++){ - if((Array[i] & 1) ==0){ -TEMP[J] =Array[i]; -J + + ; + } - } + for(inti = 0; i<length; i++){ Aarray[i]=Temp[i]; at } - - } -}
"Sword refers to offer" eight, adjust the array order so that the odd digit is preceded by even numbers