题目描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
import java.util.LinkedList;
public class Solution {
public void reOrderArray(int [] array) {
if(null == array || array.length == 0){
return;
}
LinkedList<Integer> stack = new LinkedList<Integer>();
int complexCount = 0;
for(int i = 0; i < array.length; i++){
if(array[i] % 2 != 0){
stack.add(array[i]);
}else{
stack.push(array[i]);
complexCount++;
}
}
int index = array.length - 1;
while(!stack.isEmpty()){
if(complexCount != 0){
array[index--] = stack.pop();
complexCount--;
}else{
array[index--] = stack.removeLast();
}
}
return;
}
}