Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
46 lines (46 loc) · 1.22 KB

File metadata and controls

46 lines (46 loc) · 1.22 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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++;
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.