LeetCode 905 Sort Array By Parity (Python)

Posted by 小明MaxMing on August 21, 2020

题目

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

解题思路

前后两个指针,如果前面的指向的是奇数,则从后面找到一个偶数进行交换

代码

class Solution:
    def sortArrayByParity(self, A: List[int]) -> List[int]:
        l, r = 0, len(A) - 1
        while l < r:
            if A[l] % 2:
                while l < r and A[r] % 2:
                    r -= 1
                A[l], A[r] = A[r], A[l]
            l += 1
        return A

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅