算法空间/Leetcode/二维数组中的查找
来自康健生活
题目描述
在一个二维数组中(每一个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
输入:一个二维数组和一个整数
判断:数组中是否含有该整数
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;
}
No categories