Skip to content

Commit

Permalink
Time: 311 ms (42.48%) | Memory: 96.4 MB (44.51%) - LeetSync
Browse files Browse the repository at this point in the history
  • Loading branch information
ShatilKhan committed Feb 14, 2024
1 parent 2e1540b commit 7ef7d4d
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var rearrangeArray = function(nums) {
let solution = new Solution();
return solution.rearrangeArray(nums);
};


var Solution = function() {};

Solution.prototype.rearrangeArray = function(nums) {
let ans = new Array(nums.length).fill(0);
let pos = 0, neg = 1;

for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
ans[pos] = nums[i];
pos += 2;
} else {
ans[neg] = nums[i];
neg += 2;
}
}

return ans;
};

0 comments on commit 7ef7d4d

Please sign in to comment.