当前位置:网站首页>Daily question - Search two-dimensional matrix PS two-dimensional array search
Daily question - Search two-dimensional matrix PS two-dimensional array search
2022-07-05 05:28:00 【ThE wAlkIng D】
Title Description
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 a function , Enter such a two-dimensional array and an integer , Determine whether the array contains the integer .
Problem analysis
If you want to find the fastest , You need to traverse from the lower left corner of the matrix , Because from the lower left corner , The number goes up and down , The number gets bigger to the right , When the target value is larger than the search value , Move upward , When the target value is smaller than the search value , Move right
Code instance
public class Solution {
public boolean Find(int target, int [][] array) {
int row = array.length-1;
int col = 0;
while((row >= 0)&&(col < array[0].length)){
if(array[row][col] > target){
row--;
}else if(array[row][col] < target){
col++;
}else{
return true;
}
}
return false;
}
}
边栏推荐
- High precision subtraction
- 利用HashMap实现简单缓存
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- 一个新的微型ORM开源框架
- Reverse one-way linked list of interview questions
- 游戏商城毕业设计
- [allocation problem] 455 Distribute cookies
- Add level control and logger level control of Solon logging plug-in
- YOLOv5-Shufflenetv2
- Maximum number of "balloons"
猜你喜欢
随机推荐
Es module and commonjs learning notes -- ESM and CJS used in nodejs
Haut OJ 1350: choice sends candy
[转]MySQL操作实战(三):表联结
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Solon Logging 插件的添加器级别控制和日志器的级别控制
[allocation problem] 135 Distribute candy
Es module and commonjs learning notes
Little known skills of Task Manager
剑指 Offer 53 - II. 0~n-1中缺失的数字
How can the Solon framework easily obtain the response time of each request?
High precision subtraction
挂起等待锁 vs 自旋锁(两者的使用场合)
Programmers' experience of delivering takeout
[轉]: OSGI規範 深入淺出
xftp7与xshell7下载(官网)
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
[interval problem] 435 Non overlapping interval
26、 File system API (device sharing between applications; directory and file API)
[es practice] use the native realm security mode on es
SDEI初探-透过事务看本质