class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
//初始化 i 和 j 为数组左下角元素
int i = matrix.length - 1,
int j = 0;
//如果 i 和 j 没有越界继续判断
while(i >= 0 && j < matrix[0].length){
if(matrix[i][j] > target){
//行索引向上移动一格(即 i-- ),即消去矩阵第 i 行元素
i--;
}else if(matrix[i][j] < target){
//列索引向右移动一格(即 j++ ),即消去矩阵第 j 列元素
j++;
}else{
//找到目标值,直接返回 ture
return true;
}
}
//没有找到目标值,返回 false
return false;
}
}
剑指 Offer 04. 二维数组中的查找
最新推荐文章于 2024-07-17 18:54:18 发布