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

Commitbe33c6d

Browse files
922_SortArrayByParityII922.java
1 parentf7bfa99 commitbe33c6d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎922_SortArrayByParityII922.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
3+
*
4+
* Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
5+
*
6+
* You may return any answer array that satisfies this condition.
7+
*
8+
* Example 1:
9+
* Input: [4,2,5,7]
10+
* Output: [4,5,2,7]
11+
*
12+
* Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
13+
*
14+
* Note:
15+
* 2 <= A.length <= 20000
16+
* A.length % 2 == 0
17+
* 0 <= A[i] <= 1000
18+
*/
19+
publicclassSortArrayByParityII922 {
20+
publicint[]sortArrayByParityII(int[]A) {
21+
inti =0;
22+
intj =1;
23+
intk =0;
24+
intlen =A.length;
25+
int[]B =newint[len];
26+
while (k <len) {
27+
if (isOdd(A[k])) {
28+
B[j] =A[k];
29+
j +=2;
30+
}else {
31+
B[i] =A[k];
32+
i +=2;
33+
}
34+
k++;
35+
}
36+
returnB;
37+
}
38+
39+
privatebooleanisOdd(intnum) {
40+
return (num &1) ==1;
41+
}
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp