• 输入一个整数数组,输出奇偶数相间排列的数组


    前提条件

    1. 时间复杂度:O(n)
    2. 空间复杂度:O(1)

    思路

    1. 若数组长度为偶数,那么其中奇数元素和偶数元素的个数相等;若数组长度为奇数,那么其中奇数元素和偶数元素的个数相差 1;我们先校验奇数元素个数和偶数元素个数是否合法,若不合法,则直接抛出异常;
    2. 根据数组中奇数元素的个数和偶数元素的个数的大小来判断结果数组中第一个元素应该是奇数还是偶数;
    3. 感觉代码写的很丑,暂时想不到更加优雅的写法了。。。
    import java.util.Arrays;
    
    class Solution {
    
        public static void main(String[] args) {
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{2, 2, 3})));
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{1, 2, 3, 4})));
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{1, 2, 3, 4, 4})));
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{1, 2, 3, 4, 5})));
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{1, 2, 3, 4, 5, 6})));
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{1, 2, 3, 4, 5, 6, 7})));
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{1, 2, 3, 4, 5, 6, 7, 8})));
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 8})));
            System.out.println(Arrays.toString(oddEvenHandle(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 8, 8})));
        }
    
        private static int[] oddEvenHandle(int[] array) {
            if (array == null || array.length == 0) {
                return array;
            }
    
            int oddCount = 0, evenCount = 0;
            for (int ele : array) {
                if (ele % 2 == 0) {
                    evenCount++;
                } else {
                    oddCount++;
                }
            }
    
            if (Math.abs(oddCount - evenCount) > 1) {
                throw new IllegalArgumentException(String.format("oddCount = %d, evenCount = %d", oddCount, evenCount));
            }
    
            boolean oddHead = false;
            if (oddCount >= evenCount) {
                oddHead = true;
            }
    
            int oddPoint = 0, evenPoint = 0;
            for (int i = 0; i < array.length; i++) {
                if (oddHead) {
                    if (i % 2 == 0) {
                        // 奇数
                        if (array[i] % 2 == 0) {
                            if (oddPoint < i) {
                                oddPoint = i;
                            }
                            while (array[oddPoint] % 2 == 0) {
                                oddPoint++;
                            }
                            swap(array, i, oddPoint);
                        }
                    } else {
                        // 偶数
                        if (array[i] % 2 == 1) {
                            if (evenPoint < i) {
                                evenPoint = i;
                            }
                            while (array[evenPoint] % 2 == 1) {
                                evenPoint++;
                            }
                            swap(array, i, evenPoint);
                        }
                    }
                } else {
                    if (i % 2 == 0) {
                        // 偶数
                        if (array[i] % 2 == 1) {
                            if (evenPoint < i) {
                                evenPoint = i;
                            }
                            while (array[evenPoint] % 2 == 1) {
                                evenPoint++;
                            }
                            swap(array, i, evenPoint);
                        }
                    } else {
                        // 奇数
                        if (array[i] % 2 == 0) {
                            if (oddPoint < i) {
                                oddPoint = i;
                            }
                            while (array[oddPoint] % 2 == 0) {
                                oddPoint++;
                            }
                            swap(array, i, oddPoint);
                        }
                    }
                }
            }
            return array;
        }
    
        private static void swap(int[] array, int i, int j) {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
        
    }
    
  • 相关阅读:
    uva 11080(二分图染色)
    poj 3255(次短路)
    uva 707(记忆化搜索)
    uva 436(floyd变形)
    uva 11748(求可达矩阵)
    uva 11573(bfs)
    Codeforces Round #226 (Div. 2) 解题报告
    uva 11354(最小瓶颈路--多组询问 MST+LCA倍增)
    uva 534(最小瓶颈路)
    uva 538(简单图论 入度出度)
  • 原文地址:https://www.cnblogs.com/optor/p/13185029.html
Copyright © 2020-2023  润新知