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