面試題22-調(diào)整數(shù)組元素順序,將奇數(shù)位于偶數(shù)前面

題目要求

輸入一個(gè)整數(shù)數(shù)組,實(shí)現(xiàn)一個(gè)函數(shù)來調(diào)整數(shù)組中的數(shù)字的順序,使得所有的奇數(shù)位于數(shù)組的前半部分,所有的偶數(shù)位于數(shù)組的后半部分。

題目解析

思路一:

  • 分析

題目要求我們把所有奇數(shù)放在偶數(shù)面前,那么我們可以借助兩個(gè)輔助下標(biāo)進(jìn)行性作業(yè)。

  • 代碼段
public static int[] reorder(int[] nums) {
        
        int leftIndex = 0;
        int rightIndex = 0 ;
        int temp = 0 ;
        
        if(nums == null || nums.length == 0) {
            return null ;
        }
        
        rightIndex = nums.length - 1 ;
        
        while( leftIndex < rightIndex ) {
            
            //找到左邊第一個(gè)偶數(shù)
            while(leftIndex < rightIndex && (nums[leftIndex] & 0x1) == 1) {
                leftIndex ++ ;
            }
            
            //找到右邊第一個(gè)偶數(shù)
            while(rightIndex > leftIndex && (nums[rightIndex] & 0x1) != 1) {
                rightIndex -- ;
            }
            
            //兩個(gè)坐標(biāo)重合,排序完畢
            if(leftIndex == rightIndex) {
                return nums ;
            }
            
            //交換偶數(shù)和奇數(shù)
            temp = nums[leftIndex] ;
            nums[leftIndex] = nums[rightIndex] ;
            nums[rightIndex] = temp ;
        
            leftIndex ++ ;
            rightIndex -- ;
            
        }
        
        return nums ;
    }

思路二:

  • 分析

我們可以將代碼中判斷條件(nums[leftIndex] & 0x1) == 1提取出來,這樣一來我們就可以完成一些相似的問題,
例如把正數(shù)放在復(fù)數(shù)前面,
把大寫字母放在小寫字母前面
新建一個(gè)接口,將接口里判斷的方法交給調(diào)用者去實(shí)現(xiàn)。

  • 代碼段
    public static int[] reorder1(int[] nums , IreOrder<int []> o) {
        
        int leftIndex = 0;
        int rightIndex = 0 ;
        int temp = 0 ;
        
        if(nums == null || nums.length == 0) {
            return null ;
        }
        
        rightIndex = nums.length - 1 ;
        
        while( leftIndex < rightIndex ) {
            
            //找到左邊第一個(gè)偶數(shù)
            while(leftIndex < rightIndex && o.judgeLeft(nums , leftIndex)) {
                leftIndex ++ ;
            }
            
            //找到右邊第一個(gè)偶數(shù)
            while(rightIndex > leftIndex && !o.judgeLeft(nums , rightIndex)) {
                rightIndex -- ;
            }
            
            //兩個(gè)坐標(biāo)重合,排序完畢
            if(leftIndex == rightIndex) {
                return nums ;
            }
            
            //交換偶數(shù)和奇數(shù)
            temp = nums[leftIndex] ;
            nums[leftIndex] = nums[rightIndex] ;
            nums[rightIndex] = temp ;
        
            leftIndex ++ ;
            rightIndex -- ;
            
        }
        
        return nums ;
    }

public interface IreOrder<T> {
 
    boolean judgeLeft (T data , int index) ;
    
}

測(cè)試代碼

    public static void main(String[] args) {
        
        int [] nums1 = {1,2,3,4,5,6,7} ;
        int [] nums2 = {1,22,2,5,4,3};
        int [] nums3 = null ;
        int [] nums4 = {} ;
        int [] nums5 = {1,3,5,7,9} ;
        int [] nums6 = {2,4,6,8,10} ;
        int [] nums7 = {1} ;
        
        show(reorder(nums1));
        show(reorder(nums2));
        show(reorder(nums3));
        show(reorder(nums4));
        show(reorder(nums5));
        show(reorder(nums6));
        show(reorder(nums7));
        
        System.out.println("000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
        
        IreOrder<int[]> o = (int[] nums , int leftIndex)->{
            return (nums[leftIndex] & 0x1) == 1 ;
        } ;
        
        show(reorder1(nums1 , o));
        show(reorder1(nums2 , o));
        show(reorder1(nums3 , o));
        show(reorder1(nums4 , o));
        show(reorder1(nums5 , o));
        show(reorder1(nums6 , o));
        show(reorder1(nums7 , o));
        
    }

運(yùn)行結(jié)果

1 7 3 5 4 6 2
1 3 5 2 4 22

1 3 5 7 9
2 4 6 8 10
1
000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1 7 3 5 4 6 2
1 3 5 2 4 22

1 3 5 7 9
2 4 6 8 10
1


看完整源碼戳源碼地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容