题目
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.
Example 1:
1 | Input: [3,1,2,4] |
Note:
1 <= A.length <= 50000 <= A[i] <= 5000
思路
- Approach 1: Sort
Use a custom comparator when sorting, to sort by parity.
Time Complexity: O(NlogN), Space Complexity: O(N) - Approach 2: Two Pass
Write all the even elements first, then write all the odd elements.
Time Complexity: O(N), Space Complexity: O(N) - Approach 3: In-Place
Like quicksort.
Time Complexity: O(N), Space Complexity: O(1)
代码
1 | class Solution(object): |