二分查找
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.size()==0)
return false;
if(matrix[0].size()==0)
return false;
int right = matrix.size() * matrix[0].size()-1;
int left=0;
while(left < right)
{
int mid = (left+right)/2;
int x = mid / matrix[0].size();
int y = mid % matrix[0].size();
if(target > matrix[x][y])
{
left = mid + 1;
}
if(target == matrix[x][y])
{
return true;
}
if(target < matrix[x][y])
{
right = mid - 1;
}
}
int x = left / matrix[0].size();
int y = left % matrix[0].size();
if(matrix[x][y] == target)
return true;
return false;
}
};