-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path922 Sort Array By Parity II.js
More file actions
42 lines (38 loc) · 1.07 KB
/
922 Sort Array By Parity II.js
File metadata and controls
42 lines (38 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Given an array A of non-negative integers, half of the
* integers in A are odd, and half of the integers are even.
* Sort the array so that whenever A[i] is odd, i is odd;
* and whenever A[i] is even, i is even.
* You may return any answer array that satisfies this condition.
* 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
* 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i]
* 为偶数时, i 也是偶数。
* 你可以返回任何满足上述条件的数组作为答案。
*/
/**
* Example:
* Input: [4,2,5,7]
* Output: [4,5,2,7]
* Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also
* have been accepted.
*/
/**
* @param {number[]} A
* @return {number[]}
*/
const sortArrayByParityII = (A) => {
let oddIndex = 1
let evenIndex = 0
const res = []
A.forEach((num) => {
if (num % 2 === 0) {
res[evenIndex] = num
evenIndex += 2
} else {
res[oddIndex] = num
oddIndex += 2
}
})
return res
}
console.log(sortArrayByParityII([4, 2, 5, 7]))