Skip to content

Commit 055b807

Browse files
authored
Update shuffle-the-array.py
1 parent 74b8534 commit 055b807

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

Python/shuffle-the-array.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ def shuffle(self, nums, n):
88
:type n: int
99
:rtype: List[int]
1010
"""
11-
def dest(i, n):
11+
def index(i):
1212
return 2*i if i < n else 2*(i-n)+1
1313

1414
for i in xrange(len(nums)):
15-
if nums[i] < 0:
16-
continue
1715
j = i
18-
while True:
19-
j = dest(j, n)
20-
nums[i], nums[j] = nums[j], nums[i]
21-
nums[j] = -nums[j]
22-
if i == j:
23-
break
16+
while nums[i] >= 0:
17+
j = index(j)
18+
nums[i], nums[j] = nums[j], ~nums[i] # processed
2419
for i in xrange(len(nums)):
25-
nums[i] = -nums[i]
20+
nums[i] = ~nums[i]
2621
return nums

0 commit comments

Comments
 (0)