当前位置:网站首页>每日一题-搜索二维矩阵ps二维数组的查找
每日一题-搜索二维矩阵ps二维数组的查找
2022-07-05 05:26:00 【ThE wAlkIng D】
题目描述
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
问题解析
如果要使得查找速度最快,则需要从矩阵的左下角开始遍历,因为从左下角来说,向上数字递减,向右数字变大,当目标值比搜索值大的时候,上移,当目标值比搜索值小的时候,右移
代码实例
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;
}
}
边栏推荐
- Yolov5 ajouter un mécanisme d'attention
- Gbase database helps the development of digital finance in the Bay Area
- Merge sort
- Haut OJ 1241: League activities of class XXX
- object serialization
- [allocation problem] 455 Distribute cookies
- Generate filled text and pictures
- Magnifying glass effect
- kubeadm系列-01-preflight究竟有多少check
- [speed pointer] 142 circular linked list II
猜你喜欢
随机推荐
room数据库的使用
[to be continued] [depth first search] 547 Number of provinces
Pointnet++的改进
SAP-修改系统表数据的方法
Add level control and logger level control of Solon logging plug-in
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
Bubble sort summary
Haut OJ 2021 freshmen week II reflection summary
服务熔断 Hystrix
Reader writer model
Research on the value of background repeat of background tiling
对象的序列化
Demonstration of using Solon auth authentication framework (simpler authentication framework)
National teacher qualification examination in the first half of 2022
Use of room database
Yolov5 adds attention mechanism
Improvement of pointnet++
Under the national teacher qualification certificate in the first half of 2022
On-off and on-off of quality system construction
C language Essay 1