题目来源:

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;
    }
}

 

相关文章:

  • 2021-05-24
  • 2021-10-15
  • 2022-02-23
  • 2021-04-26
  • 2021-11-08
  • 2021-05-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2021-06-20
  • 2021-10-26
  • 2022-01-27
  • 2022-12-23
相关资源
相似解决方案