JZ13-调整数组顺序使奇数位于偶数前面

调整数组顺序使奇数位于偶数前面

https://www.nowcoder.com/practice/ef1f53ef31ca408cada5093c8780f44b?tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1&tab=answerKey

class Solution {
    public void reOrderArray(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }
        int i = 0;
        int j = 0;

        for (j = i + 1; j < array.length; j++) {
            if (array[i] % 2 == 1) {  //i如果指向偶数
                i++;
                continue;
            }
            if (array[j] % 2 == 0) { //j指向奇数
                continue;  //这里不能再i++了 因为上面for也会加
            }
            int temp = array[j]; //这时就不能continue了,要开始交换

            for (int start = j - 1; start >= i; start--) {  //从j开始从右往左复制,插入排序。。(从左往右)会覆盖  2 4 5 -> 2 2 5 -> 2 2 2
                array[start + 1] = array[start];
            }
            array[i] = temp;
            i++;
        }
    }
}

全部评论

相关推荐

待现的未见之事:起码第一句要把自己的优势说出来吧。比如什么xx本27届学生,随时到岗....
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务