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

Commitf7bfa99

Browse files
905_Sort_Array_By_Parity
1 parentd25b46e commitf7bfa99

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎905_Sort_Array_By_Parity.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
classSolution {
2+
/* public int[] sortArrayByParity(int[] A) {
3+
A = Arrays.stream(A).
4+
boxed().
5+
sorted((a, b) -> Integer.compare(a% 2, b % 2)).
6+
mapToInt(i -> i).
7+
toArray();
8+
return A;
9+
}*/
10+
11+
/*public int[] sortArrayByParity(int[] A) {
12+
int[] ans = new int[A.length];
13+
int pos = 0;
14+
for (int num: A)
15+
if (num % 2 == 0)
16+
ans[pos++] = num;
17+
for (int num: A)
18+
if (num % 2 == 1)
19+
ans[pos++] = num;
20+
return ans;
21+
}*/
22+
23+
publicint[]sortArrayByParity(int[]A) {
24+
intlo =0,hi =A.length -1;
25+
while (lo <hi) {
26+
if (A[lo] %2 >A[hi] %2) {
27+
inttmp =A[hi];
28+
A[hi] =A[lo];
29+
A[lo] =tmp;
30+
}
31+
if (A[lo] %2 ==0)lo++;
32+
if (A[hi] %2 ==1)hi--;
33+
}
34+
returnA;
35+
}
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp