-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path905.py
More file actions
28 lines (23 loc) · 753 Bytes
/
905.py
File metadata and controls
28 lines (23 loc) · 753 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
# https://leetcode.com/problems/sort-array-by-parity/description/
from typing import List
class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
result = [-1] * len(nums)
even_index = 0
odd_index = len(nums) - 1
# def comparator(item1, item2):
# if item1 % 2 == 0:
# return -1
# elif item2 % 2 == 0:
# return 1
# else:
# return 0
# nums.sort(key=cmp_to_key(comparator))
for n in nums:
if n % 2 == 0:
result[even_index] = n
even_index += 1
else:
result[odd_index] = n
odd_index -= 1
return result