-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0144.Interleaving_Positive_and_Negative_Numbers.py
More file actions
54 lines (44 loc) Β· 1.43 KB
/
0144.Interleaving_Positive_and_Negative_Numbers.py
File metadata and controls
54 lines (44 loc) Β· 1.43 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Description
Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.
Wechat reply γ144γ get the latest requent Interview questions . (wechat id : jiuzhang15)
You are not necessary to keep the original order of positive integers or negative integers.
Example
Example 1
Input : [-1, -2, -3, 4, 5, 6]
Outout : [-1, 5, -2, 4, -3, 6]
Explanation : any other reasonable answer.
Challenge
Do it in-place and without extra memory.
"""
class Solution:
"""
@param: A: An integer array.
@return: nothing
ε
ζζ£θ΄ζ° partition εΌοΌηΆεεηΈεεζιθΏθ‘δΊ€ζ’γ
"""
def rerange(self, A):
if A is None or len(A) <= 1:
return
# Assume A does not contain 0
l, r = 0, len(A) - 1
while l <= r:
while l <= r and A[l] < 0:
l += 1
while l <= r and A[r] > 0:
r -= 1
if l <= r:
A[l], A[r] = A[r], A[l]
l += 1
r -= 1
neg_count = l
pos_count = len(A) - l
# the amount of pos and neg number should at most diff by one
if abs(neg_count - pos_count) > 1:
return
l = 1 if neg_count >= pos_count else 0
r = len(A) - 2 if pos_count >= neg_count else len(A) - 1
while l < r:
A[l], A[r] = A[r], A[l]
l += 2
r -= 2