We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d7e858f commit 673ad73Copy full SHA for 673ad73
Python/rearrange-array-elements-by-sign.py
@@ -3,6 +3,27 @@
3
4
# two pointers
5
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):
27
def rearrangeArray(self, nums):
28
"""
29
:type nums: List[int]
@@ -26,7 +47,7 @@ def neg():
47
# Time: O(n)
48
# Space: O(n)
49
# array, implementation
-class Solution2(object):
50
+class Solution3(object):
30
51
31
52
32
53
0 commit comments