算法空间/Leetcode/二维数组中的查找:修订间差异
来自康健生活
小 Kangkk移动页面编程语言/二维数组中的查找至算法空间/二维数组中的查找,不留重定向 |
|||
(没有差异)
| |||
2024年2月21日 (三) 12:01的版本
题目描述
在一个二维数组中(每一个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
输入:一个二维数组和一个整数
判断:数组中是否含有该整数
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;
}
No categories