当前位置:网站首页>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
边栏推荐
- Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
- 16MySQL之DCL 中 COMMIT和ROllBACK
- Handwritten Google's first generation distributed computing framework MapReduce
- Monte Carlo py solves the area problem! (save pupils Series)
- LeetCode 542、01 矩阵
- How to protect ECs from hacker attacks?
- The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
- ABAQUS script email auto notification
- Experience of mathematical modeling in 18 year research competition
- Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
猜你喜欢
Notes of Tang Shu's grammar class in postgraduate entrance examination English
[latex] 5 how to quickly write out the latex formula corresponding to the formula
RT-1052学习笔记 - GPIO架构分析
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
Customize timeline component styles
Servlet learning notes
go-zero框架数据库方面避坑指南
Numpy Index & slice & iteration
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
随机推荐
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
Matlab analytic hierarchy process to quickly calculate the weight
[PTA] l2-011 play with binary tree
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
PCA based geometric feature calculation of PCL point cloud processing (52)
Sqoop imports tinyint type fields to boolean type
LeetCode 20、有效的括号
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
Still using listview? Use animatedlist to make list elements move
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
SQL: query duplicate data and delete duplicate data
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
JDBC database addition, deletion, query and modification tool class
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
一. js的深拷贝和浅拷贝
JDBC tool class jdbcconutil gets the connection to the database
論文寫作 19: 會議論文與期刊論文的區別