Given an array A
of non-negative integers, return an array consisting of all the even(偶数) elements of A
, followed by all the odd(奇数) elements of A
.
You may return any answer array that satisfies this condition.
思路:就是把数组A里面的所有偶数的位置搞到前面去,奇数的位置换到后面去。
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Class Solution:
def sortArrayByParity(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
i=0
j=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=i+1
if A[j] % 2 ==1:
j=j-1
return A