剑指offer(13)调整数组顺序使奇数位于偶数前面
import java.util.List;
import java.util.ArrayList;
public class Solution {
public void reOrderArray(int [] array) {
List<Integer> odd = new ArrayList<Integer>();
List<Integer> even = new ArrayList<Integer>();
for(int i = 0;i<array.length;i++){
if(array[i] % 2 == 0){
even.add(array[i]);
}else{
odd.add(array[i]);
}
}
odd.addAll(even);
for(int j = 0;j < array.length;j++){
array[j] = odd.get(j);
}
}
}
/*public class Solution{
public void reOrderArray(int[] array){
for(int i = 0;i<array.length-1;i++){
for(int j =0;j<array.length-1-i;j++){
if(array[j]%2 == 0 && array[j+1]%2==1){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
}*/