当前位置:网站首页>Search 2D matrix 2
Search 2D matrix 2
2022-07-01 18:34:00 【Free dreamer】
Search for a two-dimensional matrix
This problem is equivalent to a brain teaser , Just know how to do it ,ok
Ideas :
We can set the starting point to the upper right corner at the beginning ,
Here we can divide into three situations
1.t==target
2.t>target, If t>target, Then we can get rid of this column , because t The numbers below this column are greater than t
3.t<target, If t<target Then this line can be removed . Because in this business t Maximum .
step :
1. First create two variables to represent the coordinates of the upper right corner
2. Then take the condition that the coordinates of the point cannot cross the boundary as the cycle , Start the cycle
3. With t Represents the coordinate value , First three situations .
class Solution {
public:
bool searchMatrix(vector<vector<int>>& a, int target) {
if(a.empty()||a[0].empty())return false;
int n=a.size(),m=a[0].size();
int i=0,j=m-1;
while(i<n&&j>=0){
int t=a[i][j];
if(t==target)return true;
else if(t>target)j--;
else i++;
}
return false;
}
};
边栏推荐
- This is the latest opportunity of the London bank trend
- 证券开户安全么,有没有什么样的危险呢
- Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?
- Session layer of csframework, server and client (1)
- Detailed explanation of ArrayList expansion
- Blue Bridge Cup real topic: the shortest circuit
- Static timing analysis (STA) in ic/fpga design
- Apache iceberg source code analysis: schema evolution
- Mujoco XML modeling
- Operation of cmake under win
猜你喜欢
Database - MySQL advanced SQL statement (I)
Yolov5 practice: teach object detection by hand
Mujoco XML modeling
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
Penetration practice vulnhub range Nemesis
Distributed task queue: Celery usage record
Flex layout
Static timing analysis (STA) in ic/fpga design
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
Blue Bridge Cup real topic: the shortest circuit
随机推荐
JS how to convert a string with a delimiter into an n-dimensional array
What are the six steps of the software development process? How to draw software development flow chart?
People help ant help task platform repair source code
Convert the robot's URDF file to mujoco model
An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
Develop those things: add playback address authentication to easycvr platform
Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
[CF559E]Gerald and Path
[PHP foundation] realize the connection between PHP and SQL database
必看,时间序列分析
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
Relationship between sensor size, pixel, dpi resolution, inch and millimeter
The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
[noip2015] jumping stone
Zabbix报警执行远程命令
开发那些事儿:EasyCVR平台添加播放地址鉴权
Opencv map reading test -- error resolution
js如何将带有分割符的字符串转化成一个n维数组
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)