Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit20f147e

Browse files
add 922
1 parentb50aad3 commit20f147e

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+
packagecom.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+
publicclass_922 {
23+
publicstaticclassSolution1 {
24+
publicint[]sortArrayByParityII(int[]A) {
25+
for (inti =0,j =1;i <A.length -1 &&j <A.length;) {
26+
if (A[i] %2 !=0 &&A[j] %2 ==0) {
27+
inttmp =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+
returnA;
41+
}
42+
}
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._922;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
publicclass_922Test {
9+
privatestatic_922.Solution1solution1;
10+
privatestaticint[]A;
11+
privatestaticint[]result;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_922.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
A =newint[]{4,2,5,7};
21+
result =solution1.sortArrayByParityII(A);
22+
CommonUtils.printArray(result);
23+
}
24+
25+
@Test
26+
publicvoidtest2() {
27+
A =newint[]{3,1,4,2};
28+
result =solution1.sortArrayByParityII(A);
29+
CommonUtils.printArray(result);
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp