当前位置:网站首页>力扣练习——55 搜索二维矩阵 II
力扣练习——55 搜索二维矩阵 II
2022-08-06 13:13:00 【qq_43403657】
55 搜索二维矩阵 II
1.问题描述
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。要求使用二分查找。
该矩阵具有以下特性:
每行的元素从左到右升序排列。
每列的元素从上到下升序排列。
说明:以上所说的升序,由于中间存在重复元素,因此严格来说,“升序”应该理解成“非递减”
示例:
现有矩阵 matrix 如下:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
给定 target = 5,返回 true。
给定 target = 20,返回 false。
可使用以下main函数:
int main()
{
vector<vector<int> > matrix;
int target;
int m,n,e;
cin>>m;
cin>>n;
for(int i=0; i<m; i++)
{
vector<int> aRow;
for(int j=0; j<n; j++)
{
cin>>e;
aRow.push_back(e);
}
matrix.push_back(aRow);
}
cin>>target;
bool res=Solution().searchMatrix(matrix,target);
cout<<(res?"true":"false")<<endl;
return 0;
}
2.输入说明
首先输入matrix的行数m、列数n,
然后输入m行,每行n个整数。
最后输入一个整数target。
3.输出说明
输出true或false
4.范例
输入
5 5
1 4 7 11 15
2 5 8 12 19
3 6 9 16 22
10 13 14 17 24
18 21 23 26 30
5
输出
true
5.代码
#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstring>
#include<algorithm>
using namespace std;
int binarySearch(vector<int>nums, int target) //注意,这里不能把int改成bool,否则会报错
{
int low = 0;
int high = nums.size() - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (nums[mid] < target)
low = mid + 1;
else if (nums[mid] > target)
high = mid - 1;
else
return mid;
}
return -1;
}
bool searchMatrix(vector<vector<int> >matrix, int target)
{
//每行进行二分查找
int m = matrix.size();//行数
int n = matrix[0].size();//列数
for (int i = 0; i < m; i++)
{
if (matrix[i][0] > target)//若该行第一个元素大于target,则该行及后面所有行都不用考虑
break;
if (matrix[i][n - 1] < target)//该行最后一个元素值小于target ,则继续搜下行
continue;
int col = binarySearch(matrix[i], target);//对第i行搜索target
if (col != -1)
{
//cout << "col=" << col << endl;
return true;
}
}
return false;
}
int main()
{
vector<vector<int> > matrix;
int target;
int m, n, e;
cin >> m;
cin >> n;
for (int i = 0; i < m; i++)
{
vector<int> aRow;
for (int j = 0; j < n; j++)
{
cin >> e;
aRow.push_back(e);
}
matrix.push_back(aRow);
}
cin >> target;
bool res = searchMatrix(matrix, target);
cout << (res ? "true" : "false") << endl;
return 0;
}
边栏推荐
- 报错:C:\cb\pytorch_1000000000000\work\aten\src\ATen\native\cuda\Indexing.cu:699: block: [9,0,0],
- 【Golang】判断某一类型是否实现指定接口的几种方法
- 王爽汇编语言第六章:包含多个段的程序
- 洛谷 P1776:宝物筛选 ← 多重背包问题 二进制优化
- 【Web3 系列开发教程——创建你的第一个 NFT(5)】使用 Ethers.js 铸造 NFT | 测试用例
- 你对数据库与数据处理了解吗?
- 微服务架构 | 分布式事务 - [Seata]
- 剖析SGI STL空间配置器(deallocate内存回收函数和reallocate内存扩充函数)
- EOS密钥被盗后如何恢复?
- [极客大挑战 2019]PHP 1
猜你喜欢

Kotlin-inline:你需要知道的一切(Android)

哈希表 | 有效字母异位词 | leecode刷题笔记

SQL图解面试题:如何实现精细化运营?(具体且精确的全过程详细讲解)
![[Geek Challenge 2019] BabySQL 1](/img/31/1cbe283cb2488ee1f9db79b49bbe69.png)
[Geek Challenge 2019] BabySQL 1

GDB/MI 命令总结

剖析SGI STL空间配置器(核心设计:_S_chunk_alloc函数)

多路分发器:IO复用的抽象Poller

解析隐式类型转换操作operator double() const,带你了解隐式转换危害有多大
![[TypeScript] In-depth study of TypeScript decorators](/img/c9/5246411eafe6acf84aa2cc8b3b0157.png)
[TypeScript] In-depth study of TypeScript decorators

Wang Shuang Assembly Language Chapter 6: Programs Containing Multiple Segments
随机推荐
疫情期间去英国游学留学安全吗?签证转机、保险入关~
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
【TypeScript】深入学习TypeScript命名空间
哈工大博士历时半年整理的《Pytorch常用函数手册》开放下载!内含200余个函数!...
安全第七天课后练习
BufferedReader和BufferedWriter的实现原理
vulnhub-DC-2 drone penetration record
解析隐式类型转换操作operator double() const,带你了解隐式转换危害有多大
生产级Redis 高并发分布式锁实战2:缓存架构设计问题优化
在数据中查找信号
Qt下编译警告unused parameter ,参数未使用
Full use of QT::QString
接口流量突增,如果是你,怎样做好性能调优?
Kubernetes 集群 Ingress 网关
MODBUS转PROFINET网关将电力智能监控仪表接入PROFINET网络案例
【Golang】判断某一类型是否实现指定接口的几种方法
链表 | 反转链表 | leecode刷题
分布式架构网络通信
链表 | 环形链表 | leecode刷题笔记
[TypeScript] In-depth study of TypeScript decorators