File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 153
153
890|[ Find and Replace Pattern] ( ./0890-find-and-replace-pattern.js ) |Medium|
154
154
905|[ Sort Array By Parity] ( ./0905-sort-array-by-parity.js ) |Easy|
155
155
916|[ Word Subsets] ( ./0916-word-subsets.js ) |Medium|
156
+ 922|[ Sort Array By Parity II] ( ./0922-sort-array-by-parity-ii.js ) |Easy|
156
157
925|[ Long Pressed Name] ( ./0925-long-pressed-name.js ) |Easy|
157
158
929|[ Unique Email Addresses] ( ./0929-unique-email-addresses.js ) |Easy|
158
159
966|[ Vowel Spellchecker] ( ./0966-vowel-spellchecker.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 922. Sort Array By Parity II
3
+ * https://leetcode.com/problems/sort-array-by-parity-ii/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given an array of integers nums, half of the integers in nums are odd,
7
+ * and the other half are even.
8
+ *
9
+ * Sort the array so that whenever nums[i] is odd, i is odd, and whenever
10
+ * nums[i] is even, i is even.
11
+ *
12
+ * Return any answer array that satisfies this condition.
13
+ */
14
+
15
+ /**
16
+ * @param {number[] } nums
17
+ * @return {number[] }
18
+ */
19
+ var sortArrayByParityII = function ( nums ) {
20
+ let i = 0 ;
21
+ let j = 1 ;
22
+
23
+ while ( i < nums . length && j < nums . length ) {
24
+ if ( nums [ i ] % 2 === 0 ) {
25
+ i += 2 ;
26
+ } else if ( nums [ j ] % 2 === 1 ) {
27
+ j += 2 ;
28
+ } else {
29
+ [ nums [ i ] , nums [ j ] ] = [ nums [ j ] , nums [ i ] ] ;
30
+ i += 2 ;
31
+ j += 2 ;
32
+ }
33
+ }
34
+
35
+ return nums ;
36
+ } ;
You can’t perform that action at this time.
0 commit comments