Leetcode 905 Sort Array By Parity

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/polanwind/article/details/88290556
class Solution {
    public int[] sortArrayByParity(int[] A) {
        int[] ans=new int[A.length];
        int head=0;
        int tail=A.length-1;
        int len=A.length;
        for(int i=0;i<len;++i){
            if(A[i]%2==0){
                ans[head]=A[i];
                head++;
            }
            else{
                ans[tail]=A[i];
                tail--;
            }
        }
        return ans;
    }
}

猜你喜欢

转载自blog.csdn.net/polanwind/article/details/88290556
今日推荐