算法空间/Leetcode/二维数组中的查找:修订间差异
来自康健生活
无编辑摘要 |
|||
| 第19行: | 第19行: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== 暴力解法 === | |||
<syntaxhighlight lang="javascript"> | |||
const f = (matrix, target) => { | |||
let bool = false; | |||
matrix.map(arr => { | |||
let i = arr.findIndex(o => o === target); | |||
if (i !== -1) bool === true | |||
}); | |||
return bool; | |||
}</syntaxhighlight> | |||
2024年1月21日 (日) 16:27的版本
题目描述
在一个二维数组中(每一个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
输入:一个二维数组和一个整数
判断:数组中是否含有该整数
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