Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.
Example
Given [-1, -2, -3, 4, 5, 6], after re-range, it will be [-1, 5, -2, 4, -3, 6] or any other reasonable answer.
Solution
Two pointers = , pay more attention to slide while.
while left < right
while slide left
while slide right
if left < right:
switch:
这道题没有给出正数、负数谁多谁少,所以需要先统计数量,数量多的要包着数量少的,然后数组尾部全是数量多的数
(1) 统计出来多少个负数跟正数
(2) 保证多的数从0开始,少的那,从索引1开始
(3) 左右指针从左侧出发,每次错开都跳两个
Last updated
Was this helpful?