题目链接:https://leetcode-cn.com/problems/search-a-2d-matrix/
题目如下:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
//二维转一维,二分处理
int m=matrix.size();//二维的行数
int n=matrix[0].size();//二维的列数
int low=0,high=m*n-1;
while(low<=high){
int mid=low+(high-low)/2;
int val=matrix[mid/n][mid%n];//key point
if(target==val) return true;
else if(target>val) low=mid+1;
else high=mid-1;
}
return false;
}
};