LeetCode - 922. 按奇偶排序数组 II

空间换时间–
LeetCode - 922. 按奇偶排序数组 II_第1张图片

/**
 * @param {number[]} A
 * @return {number[]}
 */
var sortArrayByParityII = function(A) {
    let ret = [];
    const len = A.length;
    let even = 0;
    let odd = 1;
    for(let i = 0; i < len; i++){
        if(A[i] % 2 === 0){
            ret[even] = A[i]
            even += 2
        }else{
            ret[odd] = A[i]
            odd += 2
        }
    }
    return ret;
};

你可能感兴趣的:(前端杂事)