LeetCode-905 Sort Array By Parity Solution (with Java)

1. Description:

2.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-02-24
 3  */
 4 class Solution {
 5     public int[] sortArrayByParity(int[] A) {
 6         
 7         int[] res = new int[A.length];
 8         int head = 0, tail = A.length - 1, count=0;
 9         int temp;
10         while (head <= tail) {
11             temp = A[count];
12             if (temp % 2 == 0)
13                 res[head++] = temp;
14             else
15                 res[tail--] = temp;
16             count++;
17 
18         }
19         return res;   
20     }
21 }

 

posted @ 2020-03-02 15:29  SheepCore  阅读(152)  评论(0)    收藏  举报