forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2149.java
27 lines (25 loc) · 796 Bytes
/
_2149.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _2149 {
public static class Solution1 {
public int[] rearrangeArray(int[] nums) {
int[] ans = new int[nums.length];
List<Integer> pos = new ArrayList<>();
List<Integer> neg = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
pos.add(nums[i]);
} else {
neg.add(nums[i]);
}
}
for (int i = 0, j = 0; i < nums.length && j < pos.size(); i++, j++) {
ans[i] = pos.get(j);
i++;
ans[i] = neg.get(j);
}
return ans;
}
}
}