题目来源:

https://leetcode-cn.com/problems/sort-array-by-parity/

题目描述:

LeetCode905. 按奇偶排序数组

代码如下:

class Solution {
    public int[] sortArrayByParity(int[] A) {
    	if(A==null||A.length==0) {
    		return A;
    	}
        int i=0,j=A.length-1;
        while(i<j) {
        	while(i<j&&(A[i]&0x1)==0) {
        		i++;
        	}
        	while(i<j&&(A[j]&0x1)!=0) {
        		j--;
        	}
        	if(i<j) {
        		int temp=A[i];
        		A[i]=A[j];
        		A[j]=temp;
        	}
        }
    	return A;
    }
}

 

相关文章: