首页 > 其他 > 详细

leetcode 922. Sort Array By Parity II

时间:2019-04-04 23:28:01      阅读:134      评论:0      收藏:0      [点我收藏+]

one pass method with wile

class Solution {
    public int[] sortArrayByParityII(int[] A) {
        int i = 1, j = 0;
        int N = A.length;
        while (i < N) {
            for (;i < N && A[i] % 2 == 1; i += 2);
            if (i >= N) return A;
            for (;j < N && A[j] % 2 == 0; j += 2);
            int t = A[i];
            A[i] = A[j];
            A[j] = t;
        }
        return A;
    }
}

and use for

class Solution {
    public int[] sortArrayByParityII(int[] A) {
        int j = 0;
        int N = A.length;
        for (int i = 1; i < N; i += 2) {
            if (A[i] % 2 == 0) {
                for (;j < N && A[j] % 2 == 0; j += 2);
                int t = A[i];
                A[i] = A[j];
                A[j] = t;
            }
        }
        return A;
    }
}

leetcode 922. Sort Array By Parity II

原文:https://www.cnblogs.com/exhausttolive/p/10657482.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!