Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bbef610

Browse files
committed
Add solution #2149
1 parent 59d562e commit bbef610

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,777 LeetCode solutions in JavaScript
1+
# 1,778 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1646,6 +1646,7 @@
16461646
2145|[Count the Hidden Sequences](./solutions/2145-count-the-hidden-sequences.js)|Medium|
16471647
2147|[Number of Ways to Divide a Long Corridor](./solutions/2147-number-of-ways-to-divide-a-long-corridor.js)|Hard|
16481648
2148|[Count Elements With Strictly Smaller and Greater Elements](./solutions/2148-count-elements-with-strictly-smaller-and-greater-elements.js)|Easy|
1649+
2149|[Rearrange Array Elements by Sign](./solutions/2149-rearrange-array-elements-by-sign.js)|Medium|
16491650
2154|[Keep Multiplying Found Values by Two](./solutions/2154-keep-multiplying-found-values-by-two.js)|Easy|
16501651
2161|[Partition Array According to Given Pivot](./solutions/2161-partition-array-according-to-given-pivot.js)|Medium|
16511652
2176|[Count Equal and Divisible Pairs in an Array](./solutions/2176-count-equal-and-divisible-pairs-in-an-array.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 2149. Rearrange Array Elements by Sign
3+
* https://leetcode.com/problems/rearrange-array-elements-by-sign/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums of even length consisting of an equal number
7+
* of positive and negative integers.
8+
*
9+
* You should return the array of nums such that the the array follows the given conditions:
10+
* 1. Every consecutive pair of integers have opposite signs.
11+
* 2. For all integers with the same sign, the order in which they were present in nums is
12+
* preserved.
13+
* 3. The rearranged array begins with a positive integer.
14+
*
15+
* Return the modified array after rearranging the elements to satisfy the aforementioned
16+
* conditions.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @return {number[]}
22+
*/
23+
var rearrangeArray = function(nums) {
24+
const positives = nums.filter(num => num > 0);
25+
const negatives = nums.filter(num => num < 0);
26+
const result = [];
27+
28+
for (let i = 0; i < positives.length; i++) {
29+
result.push(positives[i], negatives[i]);
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)