-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparity.py
More file actions
29 lines (26 loc) · 918 Bytes
/
parity.py
File metadata and controls
29 lines (26 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
New Leetcode UI: <results below>
Runtime: 60 ms, faster than 96.24% of Python online submissions for Sort Array By Parity.
"""
class Solution(object):
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
# O(N) time and space logic
n = len(A)
odd_loc = None
for i in range(n):
if A[i] % 2 == 0:
# Even condition, swap with the previous odd instance
if odd_loc is not None:
A[i], A[odd_loc] = A[odd_loc], A[i]
# Update the odd location to next
# By this time of the logic the odd instances are next items
odd_loc += 1
else:
# Odd condition, record first odd instance
if odd_loc is None:
odd_loc = i
return A