当前位置:网站首页>Leetcode 74. Search two-dimensional matrix
Leetcode 74. Search two-dimensional matrix
2022-04-23 20:26:00 【Die in a trap】
74、 Search for a two-dimensional matrix
1) Title Description
Write an efficient algorithm to judge m x n Matrix , Is there a target value . The matrix has the following characteristics :
- The integers in each row are arranged in ascending order from left to right .
- The first integer in each row is greater than the last integer in the previous row .
Example 1:

Input :matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output :true
Example 2:

Input :matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output :false
Tips :
m == matrix.lengthn == matrix[i].length1 <= m, n <= 100-10^4 <= matrix[i][j], target <= 10^4
2) analysis
Traveled through , Intra bank dichotomy .
- From the title, we can see , Traverse the matrix first by row , Matrix elements are non decreasing . Then, if the target value is less than the matrix row, traverse the first element first or greater than the last element , The target value must no longer be in the matrix ;
- If the target value may be in the matrix , Then traverse each row of the matrix , Compare the target value with the last element of the current row , Judge whether the target value may be in the current line ;
- If the target value is in the current line , Search the subscript of the corresponding element by dichotomy ;
- Judge whether the element corresponding to the searched subscript is equal to the target value .
3)C++ Code
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix[0][0]>target||matrix[matrix.size()-1][matrix[0].size()-1]<target)
return false;
int m=matrix.size();
int n=matrix[0].size();
for(int i=0;i<m;i++){
if(matrix[i][n-1]<target)
continue;
else{
int left=0;
int right=n-1;
while(left<=right){
int mid=(left+right)/2;
if(matrix[i][mid]<target)
left=mid+1;
else
right=mid-1;
}
return matrix[i][left]==target;
}
}
return false;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107701.html
边栏推荐
- Still using listview? Use animatedlist to make list elements move
- 【PTA】L2-011 玩转二叉树
- Operation of numpy array
- [stack and queue topics] - sliding window
- LeetCode 1351、统计有序矩阵中的负数
- Matlab analytic hierarchy process to quickly calculate the weight
- What is the difference between a host and a server?
- Browser - learning notes
- Linux64Bit下安装MySQL5.6-不能修改root密码
- Computing the intersection of two planes in PCL point cloud processing (51)
猜你喜欢

Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report

Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization

Sqoop imports tinyint type fields to boolean type

Commit and rollback in DCL of 16 MySQL

Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127

16MySQL之DCL 中 COMMIT和ROllBACK

Customize timeline component styles

2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real

Development of Matlab GUI bridge auxiliary Designer (functional introduction)

Browser - learning notes
随机推荐
Is the wechat CICC wealth high-end zone safe? How to open an account for securities
PCL点云处理之直线与平面的交点计算(五十三)
R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
Modeling based on catiav6
Solution to PowerDesigner's failure to connect to MySQL in x64 system
Handwritten Google's first generation distributed computing framework MapReduce
Linux64Bit下安装MySQL5.6-不能修改root密码
Confusion about thread blocking after calling the read () method of wrapper flow
Intersection calculation of straight line and plane in PCL point cloud processing (53)
Numpy - creation of data type and array
Mysql database and table building: the difference between utf8 and utf8mb4
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
SQL: query duplicate data and delete duplicate data
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
Markdown < a > tag new page open link
LeetCode 994、腐烂的橘子
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics