算法空间/Leetcode/二维数组中的查找:修订间差异
来自康健生活
创建页面,内容为“== 题目描述 == 在一个二维数组中(每一个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。<br> 输入:一个二维数组和一个整数 判断:数组中是否含有该整数 <syntaxhighlight lang="javascript"> Insert comment here </syntaxhighlight>” |
小 Kangkk移动页面算法空间/二维数组中的查找至算法空间/Leetcode/二维数组中的查找,不留重定向 |
||
| (未显示同一用户的4个中间版本) | |||
| 第1行: | 第1行: | ||
== 题目描述 == | == 题目描述 == | ||
在一个二维数组中(每一个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。<br> | 在一个二维数组中(每一个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。<br> | ||
输入:一个二维数组和一个整数 | 输入:一个二维数组和一个整数<br> | ||
判断:数组中是否含有该整数 | 判断:数组中是否含有该整数 | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
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; | |||
} | |||
</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> | |||