1.代码
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int i = matrix.size();
int j = matrix[0].size();
for(int p = 0;p < i;p ++){
int left = 0;
int right = j-1;
while(left <= right){
int mid = (left+right)/2;
if(matrix[p][mid] == target) return true;
if(matrix[p][mid] < target) left = mid+1;
else right = mid-1;
}
}
return false;
}
};
2.思路
对于每一行进行二分查找,如果找到target则返回true,如果最后仍未找到则返回false。