-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSortArrayByParity.java
More file actions
38 lines (33 loc) · 923 Bytes
/
SortArrayByParity.java
File metadata and controls
38 lines (33 loc) · 923 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
package array;
// Source : https://leetcode.com/problems/sort-array-by-parity/
// Id : 905
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-04-15
// Topic : Array
// Level : Easy
// Result : 100.00% 96.91%
class SortArrayByParity {
public int[] sortArrayByParity(int[] A) {
int i = 0;
int j = A.length - 1;
int tmp;
while (j > i) {
// do swap
if (A[i] % 2 == 1 && A[j] % 2 == 0) {
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
if (A[i] % 2 == 0) {
i++;
//in order to make sure a count is performed
continue;
} else if (A[j] % 2 == 1) {
j--;
//in order to make sure a count is performed
continue;
}
}
return A;
}
}