问题:
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:
输入:matrix = [
[1,4,7,11,15],
[2,5,8,12,19],
[3,6,9,16,22],
[10,13,14,17,24],
[18,21,23,26,30]
],
target = 5
输出:true
思路如图:

代码如下:
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null && matrix.length ==0){ //非空判断
return false
}
int left = matrix.length-1;
int up = 0;
while(left >=0 && up < matrix.length){
if(matrix[left][up] < target){
up++;
}else if(matrix[left][up] > target){
left--;
}else{
return true;
}
}
return false;
}
}
告别暴力法
465

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



