Skip to content
LC-0905 Easy LeetCode

905. Sort Array By Parity

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 76% Topics: Array, Two Pointers, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def sortArrayByParity(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        i = 0
        for j in xrange(len(A)):
            if A[j] % 2 == 0:
                A[i], A[j] = A[j], A[i]
                i += 1
        return A

Solution from kamyu104/LeetCode-Solutions · MIT