当前位置:网站首页>Sword finger offer 04 Search in two-dimensional array
Sword finger offer 04 Search in two-dimensional array
2022-07-05 04:17:00 【xzystart】
In a n * m In a two-dimensional array , Each row is sorted in ascending order from left to right , Each column is sorted in ascending order from top to bottom . Please complete an efficient function , Enter such a two-dimensional array and an integer , Determine whether the array contains the integer .
Example :
The existing matrix matrix as follows :
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
Limit :
0 <= n <= 1000
0 <= m <= 1000
class Solution {
public static boolean findNumberIn2DArray(int[][] matrix, int target) {
int cloumn= 0,row = matrix.length-1; // Position the subscript to the lower left corner of the matrix
// Because starting from the lower left corner or the upper right corner , That is, this array can be regarded as a binary search tree
while ((row>=0)&&cloumn<matrix[0].length){
// Traversing the binary search tree
if (matrix[row][cloumn] >target){
row--;
}else if (matrix[row][cloumn] < target){
cloumn++;
}else return true;
}
return false;
}
}
边栏推荐
- Burpsuite grabs app packets
- Number of possible stack order types of stack order with length n
- EasyCVR更改录像存储路径,不生成录像文件如何解决?
- Online sql to excel (xls/xlsx) tool
- How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
- Online text line fixed length fill tool
- Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
- Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
- [untitled]
- Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
猜你喜欢
Alibaba cloud ECS uses cloudfs4oss to mount OSS
As soon as I write the code, President Wang talks with me about the pattern all day
Threejs Internet of things, 3D visualization of factory
Containerd series - what is containerd?
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
[brush questions] BFS topic selection
CTF stegano practice stegano 9
Serpentine matrix
Ctfshow web entry code audit
Longyuan war "epidemic" 2021 network security competition web easyjaba
随机推荐
10种寻址方式之间的区别
On the day 25K joined Tencent, I cried
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
Get to know MySQL connection query for the first time
Online sql to excel (xls/xlsx) tool
Behavior perception system
[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
【虚幻引擎UE】实现测绘三脚架展开动画制作
Pyqt pyside custom telescopic menu bar sharing (including tutorial)
How to get the first few pieces of data of each group gracefully
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
A應用喚醒B應該快速方法
Interview byte, pass the exam and directly work on three sides. As a result, I found an architect to hang me?
[Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
Rome链分析
Network security - record web vulnerability fixes
如何优雅的获取每个分组的前几条数据
[untitled]
web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)
Threejs Internet of things, 3D visualization of factory