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.
input: 給一個沒有負數的數字陣列
output: 回傳前面是偶數後面是奇數的陣列
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.
Note:
1 <= A.length <= 5000
0 <= A[i] <= 5000
*/
/**
* @param {number[]} A
* @return {number[]}
*/
var sortArrayByParity = function(A) {
};
Edge Case
可能會有一樣的值 [1, 2, 1]
如果只有一個值 [1]
值不是數字 ( 但題目已經說一定是數字所以這種狀況就不用再判斷了 )
哪種資料結構解
這題很容易想,題目有 Array 又跟排序有關,那當然就是 Array 了
Array 遍歷ㄧ次,發現偶數就從前面放,奇數就從後面放 Big O(n)