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.
Idea: Create two new arrays, put odd even numbers into two arrays, and then put the array in.
1 Public classSolution {2 Public voidReorderarray (int[] Array) {3 intLen =Array.Length;4 int[]odd =New int[Len];5 int[]even =New int[Len];6 int[]merge =New int[Len];7 intj = 0;8 intK = 0;9 for(inti = 0;i<len;i++){Ten if(array[i]%2 = = 0){ Oneeven[j]=Array[i]; AJ + +; - } - Else{ theODD[K] =Array[i]; -k++; - } - } + for(intn = 0;n<k;n++){ -Array[n] =Odd[n]; + } A for(intm = k;m<j+k;m++){ atARRAY[M] = even[m-K]; - } - } -}
Adjust the array order so that the odd digits are preceded by even numbers