注意while的循环判断条件的先后
/** * @Description 我们写代码,靠的是技术(狗头) * @auther admin * @create 2020-07-04 20:09 */ public class Solution922 { public void swap(int[] A,int odd,int even){ int temp; temp = A[odd]; A[odd] = A[even]; A[even] = temp; } public int[] sortArrayByParityII(int[] A) { int odd = 1,even = 0; while(odd<A.length&&even<A.length){ while (odd<A.length&&A[odd]%2==1)odd+=2; while (even<A.length&&A[even]%2==0)even+=2; if(odd>A.length||even>A.length)break; swap(A,odd,even);odd+=2;even+=2; } return A; } public static void main(String[] args) { Solution922 solution922 = new Solution922(); int[] A = solution922.sortArrayByParityII(new int[]{2,3}); for(int i = 0;i<A.length;i++) System.out.print(A[i]); } }