LeetCode905.按奇偶排序数组(java实现)

题目:

代码:

class Solution {
    public int[] sortArrayByParity(int[] A) {
        int left=0;
        int right=A.length-1;
        while(left<right){
            if(A[left]%2==1&&A[right]%2==0){        
                int temp=A[left];
                A[left]=A[right];
                A[right]=temp;
            }else if(A[left]%2==0&&A[right]%2==1){  
                left++;
                right--;
            }else if(A[left]%2==0&&A[right]%2==0){
                left++;
            }else{
                right--;
            }
        }
        return A;

    }
}
发布了31 篇原创文章 · 获赞 1 · 访问量 1251

猜你喜欢

转载自blog.csdn.net/qq_45824565/article/details/104593279