当前位置:网站首页>二维数组中查找
二维数组中查找
2022-07-20 22:54:00 【ZJH'blog】
从右上角看,这个矩阵就像个二叉查找树
解
- 从右上角开始,比他小的在左边,比他大的在下面
- 为什么“比他大的不能是右边”:比如15到11到12,目标值肯定是比15小的,如果往右走,19>15
- 因为这个目标值也是int类型,所以不考虑浮点型导致的大小比较问题
1.先讨论是否空
2.最终解
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if(matrix == null | matrix.length == 0 || matrix[0].length == 0){
return false;
}
int row = 0;//第一行
int column = matrix[0].length - 1;//最大列号
// if(target > matrix[row][column]){//target超过最右上角
// return false;
// }
while(column >= 0 && row <= matrix.length - 1){
if(target > matrix[row][column]){
row++;
}
else if(target < matrix[row][column]){
column--;
}
else{
return true;
}
}
return false;
}
}
边栏推荐
- Gameframework - Preface
- 《云原生-Kubernetes篇》深入剖析Kubernetes中pod的原理
- Shell 脚本特殊变量列表
- 1720. Array after XOR decoding
- "Cloud primitives kubernetes" deeply analyzes the principle of pod in kubernetes
- Mysql database query is so slow. Besides index, what else can it do?
- Typora 收费解决方法
- 让实习生搭个Redis集群,差点把我”搭“进去~~~
- 遍历文件夹以及子文件夹的图片,利用opencv改变大小并保存直一个文件夹
- Redis cluster installation
猜你喜欢
My SQL is OK. Why is it still so slow? MySQL locking rules
Message queue rockermq development practice
第03篇:SQL语法树解析
acme自动化---免费SSL证书申请并自动续期
Games101 graphics p12 notes (geometry3)
ABAP (ALV part)
Let me tell you how a grass-roots programmer can counter attack and successfully enter bat!
1486. 数组异或操作
超简单的三管无感无刷三相电机驱动板
1720. Array after XOR decoding
随机推荐
[solved] Splunk :Non-200/201 status_ code=401; {“messages“:[{“type“:“WARN“,“text“:“call not properly
Four redis cluster schemes and their advantages and disadvantages
Typescript function extension use
acme自动化---免费SSL证书申请并自动续期
The top three suddenly changed, revealing the latest ranking of programming languages in July
ABAP (ALV part)
相关性分析及SPSS软件操作
有没有完全自主的国产化数据库技术
第06篇:池化技术
Games101 graphics P11 notes (geometry2)
GameFramework——前言
Poor CDH cluster: there are 1855 blocks with insufficient replicas in the cluster. There are 1857 blocks in the cluster. Percentage of blocks with insufficient copies: 99.89%. Critical threshold: 40.0
4种 Redis 集群方案及优缺点对比
Correlation analysis and SPSS software operation
這份 pip 使用小抄,要有全有多全!
让实习生搭个Redis集群,差点把我”搭“进去~~~
进程的五种状态
1723. 完成所有工作的最短时间
What is a video content recommendation engine?
1720. Array after XOR decoding