当前位置:网站首页>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;
}
}
边栏推荐
- [转]MySQL操作实战(一):关键字 & 函数
- Romance of programmers on Valentine's Day
- 质量体系建设之路的分分合合
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- Add level control and logger level control of Solon logging plug-in
- [turn to] MySQL operation practice (III): table connection
- [allocation problem] 135 Distribute candy
- Bubble sort summary
- SAP-修改系统表数据的方法
- 读者写者模型
猜你喜欢
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
C language Essay 1
[转]: OSGI规范 深入浅出
Hang wait lock vs spin lock (where both are used)
利用HashMap实现简单缓存
Service fusing hystrix
[interval problem] 435 Non overlapping interval
[merge array] 88 merge two ordered arrays
Little known skills of Task Manager
Pointnet++ learning
随机推荐
使用Electron开发桌面应用
远程升级怕截胡?详解FOTA安全升级
On-off and on-off of quality system construction
PMP考生,请查收7月PMP考试注意事项
Reflection summary of Haut OJ freshmen on Wednesday
Gbase database helps the development of digital finance in the Bay Area
[转]:Apache Felix Framework配置属性
Web APIs DOM node
A preliminary study of sdei - see the essence through transactions
[trans]: spécification osgi
Acwing 4300. Two operations
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
对象的序列化
挂起等待锁 vs 自旋锁(两者的使用场合)
Romance of programmers on Valentine's Day
一个新的微型ORM开源框架
Control Unit 控制部件
[allocation problem] 135 Distribute candy
xftp7与xshell7下载(官网)
Talking about JVM (frequent interview)