-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo14.java
116 lines (105 loc) · 2.3 KB
/
No14.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ween_2;
/**
* 剑指offer: 调整数组顺序使奇数位于偶数前面
* 方法:两个指针
* 测试用例:功能测试(123456,135246,1,246135,13)
* 特殊输入测试(数组为空)
* @author dingding
* Date:2017-6-21 16:19
* Declaration: All Rights Reserved!
*/
public class No14 {
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
test6();
}
//solution
private static void oddAheadOfEven(int[] arr){
if (arr == null || arr.length<1) {
return;
}
int pBegin = 0;
int pEnd = arr.length-1;
while (pBegin<pEnd){
//向后移动pBedin,直到找到偶数
while (pBegin<pEnd && (arr[pBegin]&1)!=0){ //(i&1)==0 代表i为偶数
pBegin++;
}
//向前移动pEnd,直到找到奇数
while (pBegin<pEnd && (arr[pEnd]&1)==0){
pEnd--;
}
if (pBegin<pEnd) {
int tmp = arr[pBegin];
arr[pBegin] = arr[pEnd];
arr[pEnd] = tmp;
}
}
}
//判断偶数
boolean isOdd(int n){
return (n & 1) == 0; //true 为偶数
}
/*=======================测试用例=============*/
private static void test1() {
// TODO Auto-generated method stub
int[] arr={1,2,3,4,5,6};
oddAheadOfEven(arr);
for(int number:arr){
System.out.print(number+" ");
}
System.out.println();
System.out.println("========================");
}
private static void test2() {
int[] arr={1,3,5,2,4,6};
oddAheadOfEven(arr);
for(int number:arr){
System.out.print(number+" ");
}
System.out.println();
System.out.println("========================");
}
private static void test3() {
int[] arr={2,4,6,1,3,5};
oddAheadOfEven(arr);
for(int number:arr){
System.out.print(number+" ");
}
System.out.println();
System.out.println("========================");
}
private static void test4() {
int[] arr={2};
oddAheadOfEven(arr);
for(int number:arr){
System.out.print(number+" ");
}
System.out.println();
System.out.println("========================");
}
private static void test5() {
int[] arr={1,3};
oddAheadOfEven(arr);
for(int number:arr){
System.out.print(number+" ");
}
System.out.println();
System.out.println("========================");
}
private static void test6() {
int[] arr={};
oddAheadOfEven(arr);
if (arr.length<1 || arr == null) {
System.out.print("无效数组. ");
return;
}
for(int number:arr){
System.out.print(number+" ");
}
}
}