当前位置:网站首页>LeetCode-240-搜索二维矩阵Ⅱ
LeetCode-240-搜索二维矩阵Ⅱ
2022-07-15 19:42:00 【z754916067】
题目

思路
- 看了一下数据量有10^9这么多,那必定二分查找了。这道题第一眼看很简单,再一看好像也没有那么简单。
- 不然还是从左往右开始找,没找到就从上往下开始找,找不到的话直接返回即可。虽然用了四个二分查找,但是最后还是没做出来,有个特殊用例直接把我的思路否定了- -润去题解
- 好像是对每一行进行二分查找即可,行吧我想复杂了。
错误代码(四个二分查找)
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix[0].length==1 && matrix.length==1){
if(matrix[0][0]==target) return true;
else return false;
}
//还有可能在右下角 右下角两个二分是搜索不到的
if(matrix[matrix.length-1][matrix[0].length-1]==target) return true;
//二分查找
int left=0,right = matrix[0].length-1;
while(right-left>1){
int middle = left+(right-left)/2;
if(matrix[0][middle]<target) left=middle;
else if(matrix[0][middle]>target) right=middle;
else return true;
}
//此时必有matrix[0][left]<target<matrix[0][right] 故有可能在matrix[0][left]的那列
int up = 0,down = matrix.length-1;
if(matrix[up][0]==target || matrix[down][0]==target ) return true;
//二分查找
while(down-up>1){
int middle = up+(down-up)/2;
if(matrix[middle][left]<target) up=middle;
else if(matrix[middle][left]>target) down=middle;
else return true;
}
//走到这里来说明横的二分没找到 那就竖的二分...
up = 0;
down = matrix.length-1;
//二分查找
while(down-up>1){
int middle = up+(down-up)/2;
if(matrix[middle][0]<target) up=middle;
else if(matrix[middle][0]>target) down=middle;
else return true;
}
//此时必有matrix[up][0]<target<matrix[down][0] 故有可能在matrix[up][0]的那行
left=0;
right=matrix[0].length-1;
if(matrix[0][left]==target || matrix[0][right]==target || matrix[up][left]==target || matrix[up][right]==target) return true;
while(right-left>1){
int middle = left+(right-left)/2;
if(matrix[up][middle]<target) left=middle;
else if(matrix[up][middle]>target) right=middle;
else return true;
}
return false;
}
正确代码
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix[0].length==1 && matrix.length==1){
if(matrix[0][0]==target) return true;
else return false;
}
for(int i=0;i<matrix.length;i++){
//二分查找
int left=0,right = matrix[0].length-1;
if(matrix[i][left]==target || matrix[i][right]==target) return true;
while(right-left>1){
int middle = left+(right-left)/2;
if(matrix[i][middle]<target) left=middle;
else if(matrix[i][middle]>target) right=middle;
else return true;
}
}
return false;
}
边栏推荐
- A privacy protection and authentication method for fingerprint biometrics
- Using builderoot to learn and drive development
- Can you use redis? Then come and learn about redis protocol
- Opengauss cooperates with industry innovation to build a root community of open source databases
- Codeforces Round #803 (Div. 2) D. Fixed Point Guessing
- A few minutes before work, I thoroughly understood json stringify()
- User login and registration function with verification code
- 408天勤链表插入排序与双气泡排序课后习题代码
- Codeforces Round #805 A - G
- 小目标检测2_OHEM
猜你喜欢

HCIP第八天笔记

Codeforces Round #802 C. Helping the Nature

[open source trusted privacy computing framework "argot"] ant announced the official open source for global developers
求助,更新到思源 v2.0.27,dark+ 主题自适应宽度问题

用户登录和注册功能带验证码

微服务学习

7-Redis架构设计到使用场景-缓存穿透、缓存雪崩、缓存预热、缓存降级

助力品牌洞察——消费者情绪行为分析

小目标检测2_OHEM

SQL也能玩转AI ?没错!MLOps Meetup V3 回顾|OpenMLBD+SQLFlow+Byzer
随机推荐
6-Redis架构设计到使用场景-持久化机制、缓存失效策略、缓存命中率
datax扩展vertica插件
真实案例,大学生接单被骗,希望大家不要被骗了【惨痛教训】
Grow up Summer Challenge | learn / create from the boss, and get CSDN gift bag, exclusive certificate of honor, commemorative T-shirt and medal!
迪赛智慧数——柱状图(正负条形图):2022届毕业生不同城市的期望&实际薪资
APP自动化测试框架搭建(七)--Airtest基础操作
请教一个问题, FLinkCDC同步mysql的数据,必须要root权限吗?
云文档管理软件 DocuWare Cloud 如何解决 5 大 IT 难题
网络地址转换
【MT2126】 数字游戏
Codeforces Round #802 B. Palindromic Numbers
设计稳定的微服务系统时不得不考虑的场景
openGauss 联合产业界创新,共建开源数据库根社区
Accumulate class hour experience and practice of children's programming
脚本编写规则和变量定义
QT make color selection control
【MT2109】矩形
响应式织梦模板家居家具建材类网站
Creativity of project-based learning in children's programming
Codeforces Round #803 (Div. 2) A. XOR Mixup