The title

Given an array of non-negative integers A, half of the integers in A are odd and half are even.

Sort the array so that when A[I] is odd, I is also odd; When A[I] is even, I is even.

You can return any array that meets the above criteria as an answer.

 

Example: input:,2,5,7 [4] output:,5,2,7 [4] :,7,2,5 [4],,5,4,7 [2], [2,7,4,5] will be accepted.Copy the code

Tip:

2 <= A.length <= 20000 A.length % 2 == 0 0 <= A[i] <= 1000

Their thinking

class Solution: def sortArrayByParityII(self, nums: List[int]) -> List[int]: l1 = [i for i in nums if i % 2 == 0] l2 = [i for i in nums if i % 2 != 0] # print(l1) # print(l2) resList = [] for i in l1: resList.append(i) resList.append(l2.pop()) return resList if __name__ == '__main__': Nums = [4,2,5,7] ret = Solution(). Print (ret)Copy the code