LeetCode 2149: Rearrange Array Elements by Sign (Two-Pointer Fill by Parity Indices)
LeetCode 2149ArrayTwo PointersToday we solve LeetCode 2149 - Rearrange Array Elements by Sign.
Source: https://leetcode.com/problems/rearrange-array-elements-by-sign/
English
Problem Summary
Given an array with equal counts of positive and negative numbers, reorder it so that signs alternate and the first element is positive, while preserving the relative order among positives and among negatives.
Key Insight
The target pattern fixes each position type: even indices must be positive, odd indices must be negative. So we can fill the answer directly with two write pointers.
Algorithm
1) Create ans with the same length.
2) Maintain pos = 0 (next even slot) and neg = 1 (next odd slot).
3) Scan nums once: positive goes to ans[pos], negative goes to ans[neg].
4) Move the corresponding pointer by 2 each time.
Complexity Analysis
Time: O(n).
Space: O(n) for the output array.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int[] rearrangeArray(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
int pos = 0, neg = 1;
for (int x : nums) {
if (x > 0) {
ans[pos] = x;
pos += 2;
} else {
ans[neg] = x;
neg += 2;
}
}
return ans;
}
}func rearrangeArray(nums []int) []int {
n := len(nums)
ans := make([]int, n)
pos, neg := 0, 1
for _, x := range nums {
if x > 0 {
ans[pos] = x
pos += 2
} else {
ans[neg] = x
neg += 2
}
}
return ans
}class Solution {
public:
vector rearrangeArray(vector& nums) {
int n = nums.size();
vector ans(n);
int pos = 0, neg = 1;
for (int x : nums) {
if (x > 0) {
ans[pos] = x;
pos += 2;
} else {
ans[neg] = x;
neg += 2;
}
}
return ans;
}
}; class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * n
pos, neg = 0, 1
for x in nums:
if x > 0:
ans[pos] = x
pos += 2
else:
ans[neg] = x
neg += 2
return ansvar rearrangeArray = function(nums) {
const n = nums.length;
const ans = new Array(n);
let pos = 0, neg = 1;
for (const x of nums) {
if (x > 0) {
ans[pos] = x;
pos += 2;
} else {
ans[neg] = x;
neg += 2;
}
}
return ans;
};中文
题目概述
给定一个正负数数量相同的数组,要求重排后满足:符号交替出现、首元素为正数,并且正数之间、负数之间的相对顺序都要保持不变。
核心思路
目标位置类型是固定的:偶数位放正数,奇数位放负数。于是可以直接用两个写指针分别填充答案数组。
算法步骤
1)创建结果数组 ans。
2)维护 pos = 0(下一个偶数位)和 neg = 1(下一个奇数位)。
3)线性扫描原数组:遇到正数写入 ans[pos],遇到负数写入 ans[neg]。
4)对应指针每次加 2。
复杂度分析
时间复杂度:O(n)。
空间复杂度:O(n)(输出数组)。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int[] rearrangeArray(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
int pos = 0, neg = 1;
for (int x : nums) {
if (x > 0) {
ans[pos] = x;
pos += 2;
} else {
ans[neg] = x;
neg += 2;
}
}
return ans;
}
}func rearrangeArray(nums []int) []int {
n := len(nums)
ans := make([]int, n)
pos, neg := 0, 1
for _, x := range nums {
if x > 0 {
ans[pos] = x
pos += 2
} else {
ans[neg] = x
neg += 2
}
}
return ans
}class Solution {
public:
vector rearrangeArray(vector& nums) {
int n = nums.size();
vector ans(n);
int pos = 0, neg = 1;
for (int x : nums) {
if (x > 0) {
ans[pos] = x;
pos += 2;
} else {
ans[neg] = x;
neg += 2;
}
}
return ans;
}
}; class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * n
pos, neg = 0, 1
for x in nums:
if x > 0:
ans[pos] = x
pos += 2
else:
ans[neg] = x
neg += 2
return ansvar rearrangeArray = function(nums) {
const n = nums.length;
const ans = new Array(n);
let pos = 0, neg = 1;
for (const x of nums) {
if (x > 0) {
ans[pos] = x;
pos += 2;
} else {
ans[neg] = x;
neg += 2;
}
}
return ans;
};
Comments