Prepared by an efficient algorithm to determine the mxn matrix, the presence or absence of a target value. This matrix has the following characteristics:
An integer of from left to right in each row in ascending order.
The first integer is greater than the last row of each integer previous row.
Example 1:
Input:
Matrix = [
[. 1,. 3,. 5,. 7],
[10,. 11, 16, 20 is],
[23 is, 30, 34 is, 50]
]
target =. 3
Output: true
Example 2:
Input:
Matrix = [
[. 1,. 3,. 5,. 7],
[10,. 11, 16, 20 is],
[23 is, 30, 34 is, 50]
]
target = 13 is
output: false
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/search-a-2d-matrix
class Solution { public: int n,m; int lower_bound_(vector<vector<int>>& a,int x,int y,int v){ int mid; while(x<y){ mid=x+(y-x)/2; if(a[mid/m][mid%m]>=v)y=mid; else x=mid+1; } return x; } bool searchMatrix(vector<vector<int>>& matrix, int target) { n=matrix.size(); if(n==0)return false; m=matrix[0].size(); int x=lower_bound_(matrix,0,n*m,target); if(x<n*m&&matrix[x/m][x%m]==target)return true; return false; } };