Skip to content

Commit cd6a850

Browse files
committed
Add Solution.java for 0922.Sort Array By Parity II
1 parent 0eaafc5 commit cd6a850

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[] sortArrayByParityII(int[] A) {
3+
int j = 1, length = A.length;
4+
for (int i = 0; i < length; i += 2) {
5+
if ((A[i] & 1) != 0) {
6+
while ((A[j] & 1) != 0) j += 2;
7+
8+
// Swap A[i] and A[j]
9+
int tmp = A[i];
10+
A[i] = A[j];
11+
A[j] = tmp;
12+
}
13+
}
14+
return A;
15+
}
16+
}

0 commit comments

Comments
 (0)