Skip to content

Commit c93c52d

Browse files
committed
Add solution #922
1 parent e7f49ab commit c93c52d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|
154154
905|[Sort Array By Parity](./0905-sort-array-by-parity.js)|Easy|
155155
916|[Word Subsets](./0916-word-subsets.js)|Medium|
156+
922|[Sort Array By Parity II](./0922-sort-array-by-parity-ii.js)|Easy|
156157
925|[Long Pressed Name](./0925-long-pressed-name.js)|Easy|
157158
929|[Unique Email Addresses](./0929-unique-email-addresses.js)|Easy|
158159
966|[Vowel Spellchecker](./0966-vowel-spellchecker.js)|Medium|
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
};

0 commit comments

Comments
 (0)