- 有个整数数组 ,里面有奇数和偶数,1)将所有奇数挪到偶数前面,2)保障所有奇数和偶数内部相对顺序不发生变化,3)在原数组上操作o(1)的空间复杂度
public class Array {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 8, 9, 6};
int[] ints = orderArray(arr);
System.out.println(Arrays.toString(ints));
}
public static int[] orderArray(int[] array) {
int count = 0;
int len = array.length - 1;
for (int i = 0; i <= len; i++) {
if (array[i] % 2 == 1) {
int j = i;
while (j > count) {
int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
j--;
}
count++;
}
}
return array;
}
}