二分搜索
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if (!m) return 0;
int n = matrix[0].size(), r;
if(!n) return 0;
int left(0), right(m - 1), mid;
while ((right - left)>1)
{
mid = (left + right) / 2;
if (target == matrix[mid][0])
{
return 1;
}
else if (target<matrix[mid][0])
{
right = mid;
}
else {
left = mid;
}
}
if(matrix[right][0]>target)
{
r = left;
}else{
r=right;
}
left = 0;
right = n - 1;
while ((right - left)>1)
{
mid = (left + right) / 2;
if (target == matrix[r][mid])
{
return 1;
}
else if (target<matrix[r][mid])
{
right = mid;
}
else {
left = mid;
}
}
if (matrix[r][left] == target || matrix[r][right] == target)
{
return 1;
}
return 0;
}
};