写出一个高效的算法来搜索 m × n矩阵中的值。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每行的第一个数大于上一行的最后一个整数。
样例
考虑下列矩阵:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
给出 target = 3
,返回 true
public class Solution {
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix==null||matrix.length==0){
return false;
}
int row=matrix.length;
int column=matrix[0].length;
for(int i=0;i<row;i++){
for(int j=column-1;j>=0;j--){
if(matrix[i][j]==target){
return true;
}else if(matrix[i][j]>target){
continue;
}else{
break;
}
}
}
return false;
}
}