905. Sort Array By Parity

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(object):
    def sortArrayByParity(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        ans = [0] * len(A)
        l = 0
        r = len(A) - 1
        for value in A:
            if value % 2 == 0:
                ans[l] = value
                l += 1
            else:
                ans[r] = value
                r -= 1
        return ans
                
原文地址:https://www.cnblogs.com/whatyouthink/p/13207427.html