当前位置:网站首页>每日一题-搜索二维矩阵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;
}
}
边栏推荐
- Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
- PMP candidates, please check the precautions for PMP examination in July
- Fragment addition failed error lookup
- [turn]: OSGi specification in simple terms
- PMP考试敏捷占比有多少?解疑
- TF-A中的工具介绍
- Web APIs DOM node
- lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- 发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
- Embedded database development programming (zero)
猜你喜欢

Gbase database helps the development of digital finance in the Bay Area

Simple modal box

Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!

sync.Mutex源码解读

Pointnet++的改进

Hang wait lock vs spin lock (where both are used)
![[to be continued] [depth first search] 547 Number of provinces](/img/c4/b4ee3d936776dafc15ac275d2059cd.jpg)
[to be continued] [depth first search] 547 Number of provinces

Shell Sort

Service fusing hystrix
![[depth first search] 695 Maximum area of the island](/img/08/cfff4aec667216e4f146205a12c13f.jpg)
[depth first search] 695 Maximum area of the island
随机推荐
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
浅谈JVM(面试常考)
Reverse one-way linked list of interview questions
数仓项目的集群脚本
On-off and on-off of quality system construction
Solon Auth 认证框架使用演示(更简单的认证框架)
SAP-修改系统表数据的方法
A problem and solution of recording QT memory leakage
[to be continued] [UE4 notes] L2 interface introduction
Haut OJ 1357: lunch question (I) -- high precision multiplication
PMP考试敏捷占比有多少?解疑
ssh免密登录设置及使用脚本进行ssh登录并执行指令
A three-dimensional button
Haut OJ 1350: choice sends candy
YOLOv5-Shufflenetv2
YOLOv5添加注意力机制
【ES实战】ES上的native realm安全方式使用
[interval problem] 435 Non overlapping interval
Under the national teacher qualification certificate in the first half of 2022
Embedded database development programming (zero)