
【解题思路】
对数组中每一行每一列依次查询,如果数大于target,排除该列。查询完扔没找到target,说明不存在该数。
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
int m = matrix.length;
if(m == 0) return false;
int n = matrix[0].length;
for(int i = 0; i < m ;i++)
{
for(int j = 0; j < n; j++)
{
if(matrix[i][j] == target) return true;
else if(matrix[i][j] > target) break;
}
}
return false;
}
}
这篇博客介绍了一个在二维整数数组中查找特定目标值的算法。通过遍历数组的每一行,如果当前元素大于目标值,则跳过该列,以提高查找效率。当遍历完整个数组仍未找到目标值时,返回false。这个算法适用于需要快速定位二维数组中特定值的场景。
794

被折叠的 条评论
为什么被折叠?



