[Leet Code] Sort Array By Parity

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.
class Solution(object):
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
even_writer = 0
odd_writer = len(A) -1
answer = [0] * len(A)
for i in range(len(A)):
if A[i] % 2 == 0:
answer[even_writer] = A[i]
even_writer += 1
else:
answer[odd_writer] = A[i]
odd_writer -= 1
return answer

--

--

--

Learning, and posting my findings!

Love podcasts or audiobooks? Learn on the go with our new app.

Practical advice from software developers

Things you should know, before you start learning python or javascript

Chunkbase : How To FIND CHUNK BORDERS Tutorial! 10 Simple & Easy Ways! Minecraft Bedrock Edition

Nighthawk by Envoy — Pros vs. Cons

Google Summer of Code — The complete path

hololive 1期生 時乃空在2022年1月22日(四)舉辦演唱會 Theatrical Cover Live「Role:Play」!

Right Way To Build a Geolocation App in 2022 With IP API

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Boyd

Matthew Boyd

Learning, and posting my findings!

More from Medium

Identify Complete Binary Tree — Leetcode 958

An Introduction to Linked Lists

Leetcode — Palindrome Linked List — Easy

An Introduction to Linked Lists