zoukankan      html  css  js  c++  java
  • 剑指offer之 调整奇数偶数数组位置

    package Problem14;
    
    /*
     * 问题描述:
     * 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位与数组的前半部分,所有偶数位与数组的
     * 后半部分
     */
    public class ReorderOddEven {
        public static void reOrder(int array[]) {
            int firstIndex = 0;
            int lastIndex = array.length - 1;
            if (array == null || (0 == array.length)) {
                return;
            }
            while (firstIndex < lastIndex) {
                while ((firstIndex < lastIndex) && !(isEven(array[firstIndex]))) {
                    firstIndex++;
                }
                while ((firstIndex < lastIndex) && isEven(array[lastIndex])) {
                    lastIndex--;
                }
                if (firstIndex < lastIndex) {
                    int temp = array[firstIndex];
                    array[firstIndex] = array[lastIndex];
                    array[lastIndex] = temp;
                }
            }
        }
    
        // 进行解耦操作:odd(奇数)、even(偶数)
        private static boolean isEven(int n) {
    
            return (n & 1) == 0;
        }
    
        public static void printArr(int array[]) {
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + "");
            }
        }
    

      

  • 相关阅读:
    HDU
    QDUoj GZS的三角形 棋盘里的数学 思维+杨辉三角
    HDU
    HDU
    CodeForces
    POJ
    QDUOJ 东北大炸弹 宝岛地图-枚举+数组记录+前缀和
    HDU
    QDUOJ 分辣条-01背包恰好装满情况
    HDU
  • 原文地址:https://www.cnblogs.com/toov5/p/7656977.html
Copyright © 2011-2022 走看看