算法空间/Leetcode/二维数组中的查找:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的1个中间版本)
(没有差异)

2026年1月26日 (一) 03:50的最新版本

题目描述

在一个二维数组中(每一个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
输入:一个二维数组和一个整数
判断:数组中是否含有该整数

const f = (matrix, target) => {
    const rl = matrix.length;
    const cl = matrix[0].length;
    //
    let r = 0;
    let c = cl - 1;
    while (r < rl && c >= 0) {
        if (matrix[r][c] === target) return true;
        else if (matrix[r][c] > target) --c;
        else ++r;
    }
    return false;
}

暴力解法

const f = (matrix, target) => {
    let bool = false;
    matrix.map(arr => {
        let i = arr.findIndex(o => o === target);
        if (i !== -1) bool === true
    });
    return bool;
}