当前位置:网站首页>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;
}
}
边栏推荐
- Containerd series - what is containerd?
- 美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
- Online text line fixed length fill tool
- Threejs Internet of things, 3D visualization of factory
- NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
- Threejs realizes sky box, panoramic scene, ground grass
- Bit operation skills
- [illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
- 如何实现实时音视频聊天功能
- What is the reason why the webrtc protocol video cannot be played on the easycvr platform?
猜你喜欢
A real day for Beijing programmers!!!!!
Ctfshow web entry code audit
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
Scheduling system of kubernetes cluster
【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
Fuel consumption calculator
Use of vscode software
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
JVM garbage collection
The development of mobile IM based on TCP still needs to keep the heartbeat alive
随机推荐
TPG x AIDU|AI领军人才招募计划进行中!
As soon as I write the code, President Wang talks with me about the pattern all day
Why can't all browsers on my computer open web pages
JVM garbage collection
SPI read / write flash principle + complete code
[phantom engine UE] the difference between running and starting, and the analysis of common problems
About the recent experience of writing questions
【虚幻引擎UE】实现测绘三脚架展开动画制作
kubernetes集群之调度系统
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
A application wakes up B should be a fast method
快手、抖音、视频号交战内容付费
Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route
The new project Galaxy token just announced by coinlist is gal
Pyqt5 displays file names and pictures
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Differences among 10 addressing modes
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
【thingsboard】替换首页logo的方法
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)