当前位置:网站首页>Leetcode74. Search 2D Matrix
Leetcode74. Search 2D Matrix
2022-08-01 17:59:00 【Java Full Stack R&D Alliance】
题目传送地址: https://leetcode.cn/problems/search-a-2d-matrix/
运行效率
代码如下:
class Solution {
public static boolean searchMatrix(int[][] matrix, int target) {
int row; //target所在的行
//处理边界情况
if (target > matrix[matrix.length - 1][0]) {
if (target > matrix[matrix.length - 1][matrix[0].length - 1]) {
return false;
} else {
row = matrix.length - 1;
}
}
//First use the dichotomy method to determine the target value possible line
int left = 0;
int right = matrix.length;
while (left != right) {
int mid = (left + right) / 2;
if (target == matrix[mid][0]) {
return true;
}
if (target > matrix[mid][0]) {
if(left==mid){
//如果left指针和rightWhen the pointer is encountered, it can jump out of the loop
break;
}
left = mid;
}
if (target < matrix[mid][0]) {
if(right==mid){
//如果left指针和rightWhen the pointer is encountered, it can jump out of the loop
break;
}
right = mid;
}
}
row = left;
//Then use the dichotomy method to determine the target value possible columns
left = 0;
right = matrix[0].length;
while (left != right) {
int mid = (left + right) / 2;
if (target == matrix[row][mid]) {
return true;
}
if (target > matrix[row][mid]) {
if(left==mid){
//如果left指针和rightWhen the pointer is encountered, it can jump out of the loop
break;
}
left = mid;
}
if (target < matrix[row][mid]) {
if(right==mid){
//如果left指针和rightWhen the pointer is encountered, it can jump out of the loop
break;
}
right = mid;
}
}
return false;
}
}
边栏推荐
猜你喜欢

JVM运行时数据区与JMM内存模型是什么

程序员架构修炼之道:如何设计“易理解”的系统架构?

云原生全景图详解

RecSys'22|CARCA: Cross-Attention-Aware Context and Attribute Recommendations

M1芯片电脑安装cerebro

QT_QThread thread

Xingtu has been short of disruptive products?Will this M38T from the Qingdao factory be a breakthrough?

B011 - 51-based multifunctional fingerprint smart lock

QT基础功能,信号、槽

QLineEdit学习与使用
随机推荐
SQL函数 TO_CHAR(三)
深入分析类加载器
【Day_09 0427】走方格的方案数
【TDP加码福利】COS用户实践征文月,等你来投稿!!!
DBPack SQL Tracing 功能及数据加密功能详解
小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
QT基础功能,信号、槽
频域分析实践介绍
【Day_11 0506】求最大连续bit数
计算IoU(D2L)
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
QPalette调色板、框架色彩填充
Review实战经典:2 种封装风格,你偏爱哪种?
LeaRun.net快速开发动态表单
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
自定义注解实现日志打印时屏蔽特定字段不打印
使用设备树时对应的驱动编程
Basic image processing in opencv
Go iota关键字与枚举类型实现原理是什么
AIOps智能运维的领跑者擎创科技正式入驻InfoQ 写作社区!