-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode922.java
More file actions
42 lines (39 loc) · 1.2 KB
/
LeetCode922.java
File metadata and controls
42 lines (39 loc) · 1.2 KB
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
40
41
42
import java.util.Arrays;
public class LeetCode922 {
public static void main(String[] args) {
// 输入:nums = [4,2,5,7]
// 输出:[4,5,2,7]
System.out.println(Arrays.toString(new Solution922().sortArrayByParityII(new int[] { 4, 2, 5, 7 })));
// 输入:nums = [2,3]
// 输出:[2,3]
System.out.println(Arrays.toString(new Solution922().sortArrayByParityII(new int[] { 2, 3 })));
}
}
class Solution922 {
public int[] sortArrayByParityII(int[] nums) {
int evenP = 0;
int oddP = 1;
while (evenP < nums.length && oddP < nums.length) {
while (nums[evenP] % 2 == 0) {
evenP += 2;
if (evenP >= nums.length) {
break;
}
}
while (nums[oddP] % 2 == 1) {
oddP += 2;
if (oddP >= nums.length) {
break;
}
}
if (evenP >= nums.length || oddP >= nums.length) {
break;
}
int temp;
temp = nums[evenP];
nums[evenP] = nums[oddP];
nums[oddP] = temp;
}
return nums;
}
}