-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_array_by_parity_ii.py
More file actions
36 lines (36 loc) · 1.06 KB
/
Copy pathsort_array_by_parity_ii.py
File metadata and controls
36 lines (36 loc) · 1.06 KB
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
30
31
32
33
34
35
36
class Solution:
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
x = []
even = True
odd = False
for i in range(len(nums)):
if even:
for j in range(len(nums)):
if nums[j] % 2 == 0:
even = False
odd = True
x.append(nums[j])
nums.pop(j)
break
else:
for j in range(len(nums)):
if nums[j] % 2 == 1:
even = True
odd = False
x.append(nums[j])
nums.pop(j)
break
return x
class Solution:
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
x = [0] * len(nums)
even = 0
odd = 1
for i in nums:
if i % 2 == 0:
x[even] = i
even += 2
else:
x[odd] = i
odd += 2
return x