[LeetCode]905. 按奇偶排序数组(双指针法的应用)

按奇偶排序数组

给定一个非负整数数组 A,返回一个数组,在该数组中, A 的所有偶数元素之后跟着所有奇数元素。

你可以返回满足此条件的任何数组作为答案。

示例:

输入:[3,1,2,4]
输出:[2,4,3,1]
输出 [4,2,3,1][2,4,1,3][4,2,1,3] 也会被接受。

提示:

1 <= A.length <= 5000
0 <= A[i] <= 5000




思路

1.双指针法,左右指针同时扫描,如果左边为奇数,右边为偶数,则交换,否则指针向中间靠拢

    public int[] sortArrayByParity(int[] A) {
        if (A == null || A.length == 1) {
            return A;
        }
        int left = 0;
        int right = A.length - 1;
        int temp;
        while (left < right) {
            //左边为奇数,右边为偶数,互换值
            if ((A[left] & 1) == 1 && (A[right] & 1) == 0) {
                tem = A[left];
                A[left] = A[right];
                A[right] = tem;
            } else if ((A[left] & 1) == 0) {
                //左边为偶数,指针向右移动
                left++;
            } else if ((A[right] & 1) == 1) {
                //右边为奇数,指针向左移动
                right--;
            }
        }
        return A;
    }
发布了36 篇原创文章 · 获赞 28 · 访问量 1505

猜你喜欢

转载自blog.csdn.net/tc979907461/article/details/105131252