一刷 05/2022
Time O(n)
Space O(n)
Runtime: 7 ms, faster than 58.43% of Java online submissions for Rearrange Array Elements by Sign.
Memory Usage: 230.7 MB, less than 5.18% of Java online submissions for Rearrange Array Elements by Sign.
class Solution {
public int[] rearrangeArray(int[] nums) {
if (nums == null || nums.length == 0) {
return nums;
}
int[] res = new int[nums.length];
int pos = 0, neg = 1;
for (int num : nums) {
if (num < 0) {
res[neg] = num;
neg += 2;
} else {
res[pos] = num;
pos += 2;
}
}
return res;
}
}
No comments:
Post a Comment