当前位置:网站首页>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;
}
}
边栏推荐
- QPalette palette, frame color fill
- sql添加索引
- 【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
- CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) 题解
- B001 - 基于STM32的智能生态鱼缸
- When custom annotations implement log printing, specific fields are blocked from printing
- Go GORM事务实例分析
- 今年最火爆的词:商业分析,看这一篇就够了!
- 【Day_12 0507】二进制插入
- 频域分析实践介绍
猜你喜欢
随机推荐
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) 题解
RecSys'22|CARCA: Cross-Attention-Aware Context and Attribute Recommendations
BITS Pilani|SAC-AP:基于 Soft Actor Critic 的深度强化学习用于警报优先级
银行案例|Zabbix跨版本升级指南,4.2-6.0不香吗?
关于LocalDateTime的全局返回时间带“T“的时间格式处理
sql添加索引
C language theory--a solid foundation for the written test and interview
QT_QThread线程
QT_QThread thread
QLineEdit学习与使用
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
EpiSci|片上系统的深度强化学习:神话与现实
SQL的substring_index()用法——MySQL字符串截取
SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级
B005 – 基于STC8的单片机智能路灯控制系统
md5sum源码 可多平台编译
opencv syntax Mat type summary
QT常用全局宏定义
2022年SQL经典面试题总结(带解析)
【Day_12 0507】查找组成一个偶数最接近的两个素数









