Intuition
- Simply use axillary space to store positive and negative values, then slot them in one by one.
Complexity
Runtime
Space
Code
Array
class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
pos = []
neg = []
for num in nums:
if num < 0:
neg.append(num)
else:
pos.append(num)
for i in range(len(nums)):
if i % 2 == 0:
nums[i] = pos[i // 2]
else:
nums[i] = neg[i // 2]
return numsCards
START Basic Front: Rearrange Array Elements By Sign Back: Slot in positive and negative values one by one via arrays END