当前位置:网站首页>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;
}
}
边栏推荐
- 网络安全-记录web漏洞修复
- The new project Galaxy token just announced by coinlist is gal
- 阿里云ECS使用cloudfs4oss挂载OSS
- Threejs Internet of things, 3D visualization of farm (III) model display, track controller setting, model moving along the route, model adding frame, custom style display label, click the model to obt
- Use Firefox browser to quickly pick up Web image materials
- 陇原战“疫“2021网络安全大赛 Web EasyJaba
- Machine learning decision tree
- BDF application - topology sequence
- 蛇形矩阵
- WGS84 coordinate system, web Mercator, gcj02 coordinate system, bd09 coordinate system - brief introduction to common coordinate systems
猜你喜欢
Serpentine matrix
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
What is test development? Why do so many companies hire test developers now?
Realize the attention function of the article in the applet
Is "golden nine and silver ten" the best time to find a job? Not necessarily
【thingsboard】替换首页logo的方法
[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)
Learning notes 8
Sequence diagram of single sign on Certification Center
随机推荐
Why do big companies such as Baidu and Alibaba prefer to spend 25K to recruit fresh students rather than raise wages by 5K to retain old employees?
JVM garbage collection
根据入栈顺序判断出栈顺序是否合理
Rome chain analysis
Rust区块琏开发——签名加密与私钥公钥
A solution to the problem that variables cannot change dynamically when debugging in keil5
Sequence diagram of single sign on Certification Center
[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)
Threejs implements labels and displays labels with custom styles
Rome链分析
Three level linkage demo of uniapp uview u-picker components
Online text line fixed length fill tool
web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)
Hexadecimal to decimal
25K 入职腾讯的那天,我特么哭了
如何优雅的获取每个分组的前几条数据
在线SQL转Excel(xls/xlsx)工具
电源管理总线 (PMBus)
[brush questions] BFS topic selection
网络安全-记录web漏洞修复