-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathExchangeOddEven.java
47 lines (42 loc) · 1.2 KB
/
ExchangeOddEven.java
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
43
44
45
46
47
package offer.problem21;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA
*
* @Author yuanhaoyue swithaoy@gmail.com
* @Description 21. 调整数组顺序使奇数位于偶数前面
* @Date 2018-12-12
* @Time 16:35
*/
public class ExchangeOddEven {
public static int[] exchange1(int[] nums) {
int oddIndex = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 1) {
int temp = nums[i];
nums[i] = nums[oddIndex];
nums[oddIndex] = temp;
oddIndex++;
}
}
return nums;
}
public static int[] exchange2(int[] nums) {
int low = 0, high = nums.length - 1;
while (low < high) {
while (low < high && nums[low] % 2 != 1) {
low++;
}
while (low < high && nums[high] % 2 != 0) {
high--;
}
int temp = nums[low];
nums[low] = nums[high];
nums[high] = temp;
}
return nums;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(exchange2(new int[]{1, 2, 8, 9, 7, 5, 3, 1, 2, 4, 6, 8})));
}
}