905. Sort Array By Parity
🟩 Easy
Question
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.Two Pointer Approach
def sortArrayByParity(A: List[int]) -> List[int]:
i,j = 0, 0
while j < len(A):
if A[i] % 2 == 0:
i += 1
j += 1
elif A[j] % 2 == 0:
A[i], A[j] = A[j], A[i]
i += 1
else:
j += 1
return ASort
Last updated