Sort Array By Parity II
给一个数组, 一半是奇数, 一半是偶数. 按照index的奇偶, 分配数字的奇偶.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Solution { public int[] sortArrayByParityII(int[] nums) { int[] res = new int[nums.length]; int i = 0; int j = 1; for(int n : nums){ if(n % 2 == 0){ res[i] = n; i+=2; } else { res[j] = n; j+=2; } } return res; } } |