9, adjustment of the array, the preceding odd, even-after

Subject description:

Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array,

And the relative position between the odd-odd, even-even and even all the second half of the array is located, and to ensure that the same

 public void reOrderArray(int [] array) {
        int [] temp=new int[array.length];
        int count=0;
        int t=0;
        for (int i : array) {
            if(i%2!=0){
                array[count++]=i;
            }else{
                temp[t++]=i;
            }
        }
        int i=0;
        while (i<t) {
            array[count]=temp[i++];
            count++;
        }
    }

Problem-solving ideas two (chapai thought): If encountered odd, an odd move forward to the front behind

public static void reOrderArray2(int [] array) {
        for (int i = 1; i < array.length; i++) {
            int insertValue=array[i];
            if(insertValue%2==1){ 
                               //注意这里,先判断i是否出界
                while(i>0 && array[i-1]%2==0){
                    array[i]=array[i-1];
                    i--;
                }
                array[i++]=insertValue;
            }
        }
        
    }

 

Guess you like

Origin www.cnblogs.com/kobe24vs23/p/11334985.html