-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreorderArray.java
More file actions
46 lines (46 loc) · 1.22 KB
/
reorderArray.java
File metadata and controls
46 lines (46 loc) · 1.22 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
43
44
45
46
/*
public class Solution {
public void reOrderArray(int [] array) {
for(int i=0;i<array.length;i++)
{
for(int j=array.length-1;j>i;j--)
{
if(array[j]%2==1&&array[j-1]%2==0)//遇到奇数,前面是一个偶数,可以互换位置。
{
int temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
}
}
}
}
}
*/
import java.util.ArrayList;
import java.util.List;
public class Solution{
public void reOrderArray(int [] array)
{
List<Integer> odd = new ArrayList<Integer>();//奇数
List<Integer> even = new ArrayList<Integer>();//偶数
int n = array.length;
int temp = 0;
for(int i = 0;i < n;i++){
temp = array[i];
if(temp % 2 == 1){
odd.add(temp);
}else{
even.add(temp);
}
}
int m = 0;
for(int j = 0;j < odd.size();j++){
array[m] = odd.get(j);
m++;
}
for(int k = 0;k < even.size();k++){
array[m] = even.get(k);
m++;
}
}
}