使用到 CollectionAssert 紀錄一下
使用 LeetCode 905. Sort Array By Parity 練習
https://leetcode.com/problems/sort-array-by-parity/
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:
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.
寫測試部分 :
[TestMethod] public void SolutionSortArrayByParity_3_1_2_4_ShouldBe_2_4_3_1() { int[] A = new int[]{3,1,2,4}; Solution solution = new Solution(); var actual = solution.SortArrayByParity(A); var expected = new int[] {2, 4, 3, 1}; CollectionAssert.AreEqual(expected, actual); }
Solution :
public class Solution { public int[] SortArrayByParity(int[] A) { return A.OrderBy(x => (x % 2==0)?0:1).ToArray(); } }