leetcode JS部分题目 0512

256 阅读3分钟
/**
 * leetcode 2619. 数组原型对象的最后一个元素
 * 请你编写一段代码实现一个数组方法,使任何数组都可以调用 array.last() 方法,这个方法将返回数组最后一个元素。如果数组中没有元素,则返回 -1 。
 * @returns {*}
 */
Array.prototype.last = function() {
    console.log(this); // 此处的this代表数组本身,因为是数组调用的last方法
    return this.length > 0 ? this[this.length - 1]: -1;
};

console.log([1,2,3].last()); // 3


/**
 * leetcode 2620. 计数器
 * 请你编写并返回一个 计数器 函数,它接收一个整型参数 n 。这个 计数器 函数最初返回 n,每次调用它时返回前一个值加 1 的值 ( n ,  n + 1 ,  n + 2 ,等等)。
 * @type {number}
 */
var createCounter = function(n) {
    return function() {
        return n++; // 注意n++的含义,它代表本次使用的值是n,并且对n进行重新赋值n = n + 1;
    };
};

// 不理解n++的可以执行下面的这个语句试试
let n = 0;
console.log(n++); // 0
console.log(n) // 1

/**
 * leetcode 2621. 睡眠函数
 * 请你编写一个异步函数,它接收一个正整数参数 millis ,并休眠这么多毫秒。要求此函数可以解析任何值。
 */
async function sleep(millis) {
    await new Promise((resolve) => {
        setTimeout(() => {resolve()}, millis);
    })
}

/**
 * leetcode 2626. 数组归约运算
 * 请你编写一个函数,它的参数为一个整数数组 nums 、一个计算函数 fn 和初始值 init 。返回一个数组 归约后 的值。
 */
var reduce = function(nums, fn, init) {
    // 解题思路:直接使用一个for循环,把上次计算出的结果保留,在下一次计算中进行使用
    let len = nums.length;
    for(let i = 0;i<len;i++) {
        init = fn(init, nums[i]);
    }
    return init;
    // return nums.reduce((pre,cur) => {
    //     return fn(pre, cur)
    // }, init)
};

/**
 * leetcode 2629. 复合函数
 * 请你编写一个函数,它接收一个函数数组 [f1, f2, f3,…], fn] ,并返回一个新的函数 fn ,它是函数数组的 复合函数 。
 */
var compose = function(functions) {
    return function(x) {
        let len = functions.length;
        let res = x;
        // 从后往前执行的
        for(let i = len -1;i>=0;i--){
            res = functions[i](res);
        }
        return res;
    }
};

/**
 * leetcode 2634. 过滤数组中的元素
 * 请你编写一个函数,该函数接受一个整数数组参数 arr 和一个过滤函数 fn,并返回一个过滤后元素数量较少或元素数量相等的新数组。
 */
var filter = function(arr, fn) {
    const res = [];
    arr.forEach((item, index) => {
        // 注意传入两个参数
        if(fn(item, index)) {
            res.push(item)
        }
    })
    return res;
};

/**
 * leetcode 2635. 转换数组中的每个元素
 */
var map = function(arr, fn) {
    const res = [];
    arr.forEach((item,index) => {
        res.push(fn(item, index))
    })
    return res;
};

/**
 * leetcode 2624. 蜗牛排序
 */
Array.prototype.snail = function(rowsCount, colsCount) {
    const len = this.length;
    if (rowsCount * colsCount !== len) return [];
    let res = [];
    let index = 0;
    let isOdd = true;
    // 解题思路:分别操控横纵坐标的移动,来构建这个数组,同时通过奇偶行来控制是从前向后遍历,还是从后向前遍历
    for(let j = 0;j < colsCount;j++){
        if(isOdd) {
            for(let i =0;i < rowsCount;i++) {
                if(!res[i]) res[i] = [];
                res[i][j] = this[index++];
            }

        } else {
            for(let i = rowsCount - 1;i >=0;i--) {
                if(!res[i]) res[i] = [];
                res[i][j] = this[index++];
            }
        }
        isOdd = !isOdd;
    }
    return  res;
}

console.log([19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15].snail(5,4));

/**
 * leetcode 2631. 分组
 */
Array.prototype.groupBy = function(fn) {
    const res = {};
    const len = this.length;
    for(let i = 0;i<len;i++) {
        const val = fn(this[i]);
        if(!res[val]) res[val] = [];
        res[val].push(this[i]);
    }
    return res;
};