Skip to content
LC-0922 Easy LeetCode

922. Sort Array By Parity II

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

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

Solution from kamyu104/LeetCode-Solutions · MIT