当前位置:网站首页>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.length
n == matrix[i].length
1 <= 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
边栏推荐
- Some basic knowledge of devexpress report development
- 【PTA】L2-011 玩转二叉树
- 一. js的深拷贝和浅拷贝
- Commit and rollback in DCL of 16 MySQL
- Linux64Bit下安装MySQL5.6-不能修改root密码
- R language survival package coxph function to build Cox regression model, ggrisk package ggrisk function and two_ Scatter function visualizes the risk score map of Cox regression, interprets the risk
- [stack and queue topics] - sliding window
- Three. Based on ply format point cloud voxel model JS upload interface writing
- Rt-1052 learning notes - GPIO architecture analysis
- 上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
猜你喜欢
LeetCode 994、腐烂的橘子
[stack and queue topics] - sliding window
Numpy - creation of data type and array
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
LeetCode 542、01 矩阵
16MySQL之DCL 中 COMMIT和ROllBACK
Modeling based on catiav6
Some basic configurations in interlij idea
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
RT-1052学习笔记 - GPIO架构分析
随机推荐
A useless confession artifact
An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
Monte Carlo py solves the area problem! (save pupils Series)
Scripy tutorial - (2) write a simple crawler
Devexpress 14.1 installation record
Research on open source OCR engine
. Ren -- the intimate artifact in the field of vertical Recruitment!
R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
Tensorflow 2 basic operation dictionary
I JS deep copy and shallow copy
Es index (document name) fuzzy query method (database name fuzzy query method)
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
How can matlab obtain the truncated image in trainingimagelabeler
LeetCode 1351、统计有序矩阵中的负数
Notes of Tang Shu's grammar class in postgraduate entrance examination English
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
ArcGIS js api 4. X submergence analysis and water submergence analysis
[PTA] l2-011 play with binary tree
Markdown < a > tag new page open link
Is the wechat CICC wealth high-end zone safe? How to open an account for securities