Skip to content

Commit e2d30a3

Browse files
authored
Create 2149-rearrange-array-elements-by-sign.js
1 parent ebe45ff commit e2d30a3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
const rearrangeArray = function(nums) {
6+
const pos = [], neg = []
7+
for(let e of nums) {
8+
if(e >= 0) pos.push(e)
9+
else neg.push(e)
10+
}
11+
const res = []
12+
for(let i = 0; i < nums.length; i++) {
13+
if(i % 2 === 0) res.push(pos[~~(i / 2)])
14+
else res.push(neg[~~(i / 2)])
15+
}
16+
return res
17+
};

0 commit comments

Comments
 (0)