classSolution(object): defsortArrayByParityII(self, A): """ :type A: List[int] :rtype: List[int] """ A.sort(key=lambda x:x%2) N=len(A) res=[] for i in range(N//2): res.append(A[i]) res.append(A[N-1-i]) return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution(object): defsortArrayByParityII(self, A): """ :type A: List[int] :rtype: List[int] """ even=[x for x in A if x%2==0] odd=[x for x in A if x%2!=0]
res=[] iseven=True while even or odd: if iseven: res.append(even.pop()) else: res.append(odd.pop())
iseven=not iseven return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution(object): defsortArrayByParityII(self, A): """ :type A: List[int] :rtype: List[int] """ even=[x for x in A if x%2==0] odd=[x for x in A if x%2!=0]
res=[] iseven=True for i in range(len(even)): res.append(even[i]) res.append(odd[i]) return res