题目
905. Sort Array By Parity
解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ i, j = 0, len(A) - 1 while i < j: if A[i] % 2 > A[j] % 2: A[i], A[j] = A[j], A[i]
if A[i] % 2 == 0: i += 1 if A[j] % 2 == 1: j -= 1
return A
|
1 2 3 4 5 6 7 8 9 10 11
| class Solution { public: vector<int> sortArrayByParity(vector<int>& A) { int len = A.size()-1; for(int i = 0;i < len;i++){ if(A[i] % 2 == 1) swap(A[i--],A[len--]); } return A; } };
|