JZ13-调整数组顺序使奇数位于偶数前面
调整数组顺序使奇数位于偶数前面
https://www.nowcoder.com/practice/ef1f53ef31ca408cada5093c8780f44b?tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1&tab=answerKey
class Solution { public void reOrderArray(int[] array) { if (array == null || array.length == 0) { return; } int i = 0; int j = 0; for (j = i + 1; j < array.length; j++) { if (array[i] % 2 == 1) { //i如果指向偶数 i++; continue; } if (array[j] % 2 == 0) { //j指向奇数 continue; //这里不能再i++了 因为上面for也会加 } int temp = array[j]; //这时就不能continue了,要开始交换 for (int start = j - 1; start >= i; start--) { //从j开始从右往左复制,插入排序。。(从左往右)会覆盖 2 4 5 -> 2 2 5 -> 2 2 2 array[start + 1] = array[start]; } array[i] = temp; i++; } } }