Skip to content

Commit 673ad73

Browse files
authored
Update rearrange-array-elements-by-sign.py
1 parent d7e858f commit 673ad73

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

Python/rearrange-array-elements-by-sign.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@
33

44
# two pointers
55
class Solution(object):
6+
def rearrangeArray(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: List[int]
10+
"""
11+
pos, neg = 0, 1
12+
result = [0]*len(nums)
13+
for x in nums:
14+
if x > 0:
15+
result[pos] = x
16+
pos += 2
17+
else:
18+
result[neg] = x
19+
neg += 2
20+
return result
21+
22+
23+
# Time: O(n)
24+
# Space: O(1)
25+
# generator
26+
class Solution2(object):
627
def rearrangeArray(self, nums):
728
"""
829
:type nums: List[int]
@@ -26,7 +47,7 @@ def neg():
2647
# Time: O(n)
2748
# Space: O(n)
2849
# array, implementation
29-
class Solution2(object):
50+
class Solution3(object):
3051
def rearrangeArray(self, nums):
3152
"""
3253
:type nums: List[int]

0 commit comments

Comments
 (0)