题目描述:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
代码如下
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
if(matrix.size()==0||matrix[0].size()==0)
return false;
int i=0,j=matrix[0].size()-1;
while(i<matrix.size()&&j>=0)
{
if(matrix[i][j]==target)
return true;
else if(matrix[i][j]<target)
i++;
else
j--;
}
return false;
}