Skip to content

Commit 20f147e

Browse files
add 922
1 parent b50aad3 commit 20f147e

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | O(n) | O(1) | |Easy|
3233
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java) | O(n) | O(n) | |Easy|
3334
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
3435
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 922. Sort Array By Parity II
5+
*
6+
* Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
7+
* Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
8+
* You may return any answer array that satisfies this condition.
9+
*
10+
* Example 1:
11+
*
12+
* Input: [4,2,5,7]
13+
* Output: [4,5,2,7]
14+
* Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
15+
*
16+
*
17+
* Note:
18+
* 2 <= A.length <= 20000
19+
* A.length % 2 == 0
20+
* 0 <= A[i] <= 1000
21+
* */
22+
public class _922 {
23+
public static class Solution1 {
24+
public int[] sortArrayByParityII(int[] A) {
25+
for (int i = 0, j = 1; i < A.length - 1 && j < A.length;) {
26+
if (A[i] % 2 != 0 && A[j] % 2 == 0) {
27+
int tmp = A[i];
28+
A[i] = A[j];
29+
A[j] = tmp;
30+
i += 2;
31+
j += 2;
32+
}
33+
while (i < A.length - 1 && A[i] % 2 == 0) {
34+
i += 2;
35+
}
36+
while (j < A.length && A[j] % 2 != 0) {
37+
j += 2;
38+
}
39+
}
40+
return A;
41+
}
42+
}
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._922;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _922Test {
9+
private static _922.Solution1 solution1;
10+
private static int[] A;
11+
private static int[] result;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _922.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[]{4, 2, 5, 7};
21+
result = solution1.sortArrayByParityII(A);
22+
CommonUtils.printArray(result);
23+
}
24+
25+
@Test
26+
public void test2() {
27+
A = new int[]{3, 1, 4, 2};
28+
result = solution1.sortArrayByParityII(A);
29+
CommonUtils.printArray(result);
30+
}
31+
}

0 commit comments

Comments
 (0)