-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution922.java
More file actions
39 lines (34 loc) · 850 Bytes
/
Solution922.java
File metadata and controls
39 lines (34 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package algorithm.leetcode;
/**
* @author: mayuan
* @desc:
* @date: 2019/01/15
*/
public class Solution922 {
public int[] sortArrayByParityII(int[] A) {
if (null == A || 1 >= A.length) {
return A;
}
// 偶数指针(even pointer)
int i = 0;
// 奇数指针(odd pointer)
int j = 1;
while (i < A.length && j < A.length) {
while (i < A.length && 0 == (A[i] & 1)) {
i += 2;
}
while (j < A.length && 1 == (A[j] & 1)) {
j += 2;
}
if (i < A.length && j < A.length) {
swap(A, i, j);
}
}
return A;
}
public void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}