1. 程式人生 > >leetcode 922. 按奇偶排序陣列 II javascript

leetcode 922. 按奇偶排序陣列 II javascript

不多說,上程式碼

/**
 * @param {number[]} A
 * @return {number[]}
 */
var sortArrayByParityII = function(A) {
  let tempArr = []
  let t = 0;
  let a = 1;
  for (let i = 0; i < A.length; i++) {
    if (A[i] % 2 == 0) {
      tempArr[t] = A[i];
      t += 2;
    } else {
      tempArr[a] = A[i];
      a += 2;
    }
  }
  return tempArr
};