题目:
调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序。
如数组:[1,2,3,4,5,6,7,8,9]
调整后可能是:[1, 9,3,7,5, 6, 4, 8, 2]
代码:
import java.util.Arrays;public class Main {public static void main(String[] args) {int[] array={1,2,3,4,5,6,7,8,9};int left=0;int right=array.length-1;//下标while(left<right){//加上left<right避免数组越界while(left<right&&array[left] % 2 != 0) {left++;}while(left<right&&array[right] % 2 == 0){right--;}//此时array[left]已经是偶数了,array[right]已经是奇数了int tmp = array[left];array[left] = array[right];array[right] = tmp;}System.out.println(Arrays.toString(array));}
}