File tree 2 files changed +35
-1
lines changed
2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,777 LeetCode solutions in JavaScript
1
+ # 1,778 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1646
1646
2145|[ Count the Hidden Sequences] ( ./solutions/2145-count-the-hidden-sequences.js ) |Medium|
1647
1647
2147|[ Number of Ways to Divide a Long Corridor] ( ./solutions/2147-number-of-ways-to-divide-a-long-corridor.js ) |Hard|
1648
1648
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|
1649
1650
2154|[ Keep Multiplying Found Values by Two] ( ./solutions/2154-keep-multiplying-found-values-by-two.js ) |Easy|
1650
1651
2161|[ Partition Array According to Given Pivot] ( ./solutions/2161-partition-array-according-to-given-pivot.js ) |Medium|
1651
1652
2176|[ Count Equal and Divisible Pairs in an Array] ( ./solutions/2176-count-equal-and-divisible-pairs-in-an-array.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments