Illustration of LeetCode brush problem plan
1. Write it first
Hand-drawn comic book series officially launched!!” LeetCode brush plan “here it comes!!
Today is the 15th issue, strive for a daily issue, at most two days, welcome everyone to supervise me…
This issue is a matrix array binary lookup!!
2, the topic
So let’s look at the problem first,
3, the body
Let’s see:
First of all, the more difficult problem should be array and matrix conversion problem ~ formula is fixed, index quotient column number is the x-coordinate, index complementary column number is the y-coordinate.
4, code,
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty()||matrix[0].empty()) return false;
int m=matrix[0].size(),n=matrix.size();
int left=0;
int right=m*n- 1;
while(left<right){
int mid=left+right>>1;
if(matrix[mid/m][mid%m]>=target){
right=mid;
}
else{
left=mid+1; }}if(matrix[left/m][left%m]==target) return true;
return false; }};Copy the code
If lucky enough to help you, please help me a [praise], to a [attention]! I would appreciate an encouragement along the way.
If you want more resources, please follow @I’m Guan Xiaoliang, MAX~