当前位置:网站首页>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;
}
}
边栏推荐
- 2022年SQL大厂高频实战面试题(详细解析)
- 顺序表的简单描述及代码的简单实现
- Basic image processing in opencv
- 加州大学|通过图抽象从不同的第三人称视频中进行逆强化学习
- 8月微软技术课程,欢迎参与
- CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) Solution
- B011 - 51-based multifunctional fingerprint smart lock
- SQL函数 TO_CHAR(三)
- opencv real-time face detection
- 【无标题】setInterval和setTimeout详解
猜你喜欢
随机推荐
后台管理系统的权限思路
golang json 返回空值
【Day_12 0507】二进制插入
Go iota关键字与枚举类型实现原理是什么
加州大学|通过图抽象从不同的第三人称视频中进行逆强化学习
Detailed explanation of the working principle of crystal oscillator
C语言理论--笔试面试基础稳固
Shell nl命令详解(显示行号、读取文件)
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
百度网盘下载速度提升100倍
网上开户佣金万一靠谱吗,网上开户安全吗
SQL函数 TO_CHAR(一)
SQL的substring_index()用法——MySQL字符串截取
快速抽取resnet_v2_152中间的特征层
XAML WPF item groupBox control
JVM运行时数据区与JMM内存模型是什么
【Day_11 0506】求最大连续bit数
TCP百万并发服务器优化调参
基于ORB-SLAM2的改进代码
QLineEdit学习与使用









