Skip to content

Commit

Permalink
Create sort-array-by-parity.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Sep 16, 2018
1 parent fe9e146 commit d9529cd
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Python/sort-array-by-parity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Time: O(n)
# Space: O(1)

# 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.
#
# Example 1:
#
# 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.
#
# Note:
# - 1 <= A.length <= 5000
# - 0 <= A[i] <= 5000

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

0 comments on commit d9529cd

Please sign in to comment.