題目大意:
輸入一個整數數組,實現一個函數來調整該數組中數字的順序,使得所有的奇數位於數組的前半部分,所有的偶數位於位於數組的後半部分,並保證奇數和奇數,偶數和偶數之間的相對位置不變。
其實這道題目,很簡單,用一個輔助空間的話。
但是如果只是原地移動的話,就得好好想想了。其實就是座標的標記和交換值。
public class Solution {
public void reOrderArray(int [] array) {
int s=0;
int e=0;
int i=0;
while(i<array.length){
s=i;
while(i<array.length && array[i]%2==0){
i++;
}
e=i;
if(e==array.length){
break;
}
i=s+1;
while(s<e){
int temp=array[e];
array[e]=array[s];
array[s]=temp;
s++;
}
}
}
}