[LeetCode] Search a 2D Matrix 二分搜索
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
在一个矩阵中确定是否存在某个数,这个矩阵有规律:一个排序好的数组,一行一行地填入矩阵。解题思路就是先二分查找会在哪一行,然后二分查找找是否存在。
#include<iostream>
#include<vector>
using namespace std; class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int m=matrix.size();
if(m<) return false;
int n=matrix[].size();
int tm;
//cout<<"m:"<<m<<" n:"<<n<<endl;
if(m==) tm=;
else{
if(matrix[m-][]<=target) tm=m-;
else{
int lft=,rgt=m-;
tm =;
do{
if(matrix[tm+][]>target) break;
int mid = (lft+rgt)/;
if(matrix[mid][]>target) rgt=mid;
else lft=mid;
tm = lft;
}while(lft+<rgt);
}
}
int lft=,rgt=n-;
if(matrix[tm][rgt]==target||matrix[tm][lft]==target) return true;
if(matrix[tm][rgt]<target) return false;
int tn=lft;
do{
int mid=(lft+rgt)/;
if(matrix[tm][mid]>target) rgt = mid;
else lft=mid;
tn = lft;
if(matrix[tm][tn]==target) return true;
}while(lft+<rgt);
return false;
}
}; int main()
{
vector<vector<int> > matrix{{, , , },{, , , },{, , , }};
Solution sol;
cout<<sol.searchMatrix(matrix,)<<endl;
return ;
}
[LeetCode] Search a 2D Matrix 二分搜索的更多相关文章
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LeetCode: Search a 2D Matrix 解题报告
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- LeetCode——Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [Leetcode] search a 2d matrix 搜索二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
随机推荐
- DNS服务初步搭建
一.准备DNS服务环境 选择 bind dns服务软件包 直接yum安装 bind 和 bind-utils 工具包,测试机器安装bind-utils测试工具包. 服务程序名为 named 二.配置D ...
- Node 操作MySql数据库
1, 需要安装 MySQL 依赖 => npm i mysql -D 2, 封装一个工具类 mysql-util.js // 引入 mysql 数据库连接依赖 const mysql = re ...
- 解决Linux使用php命令 -base comment not found并安装composer
获取php的安装目录 使用 find / -name php.ini 查看php的安装位置 /usr/local/php/lib/php.ini # cd 到/usr/local/php/lib/ph ...
- linux批量替换
sed -i "s/李三/李四/g" -r result/* 将result文件夹下的所有文件中的李三替换成李四 sed命令下批量替换文件内容 格式: sed -i ...
- iOS SDK中使用NSXMLParser解析XML(iphone网络篇三)
iOS SDK的NSXMLParser解析XML文档是事件驱动模式的,即采用SAX方式来解析XML格式文档.NSXMLParser在处理XML文档的过程中当遇到一些要素(元素.属性.CDATA块.评论 ...
- HDU2586 How far away ?
一.描述 很久没写代码了,在之前一直在参与准备ASC比赛和美赛,现在又重新捡起来.目标是两个月后的邀请赛. 这题是树链拋分解决LCA问题的一个模板题. 首先介绍下树链拋分的基本思想. 对于任意一颗树, ...
- vba中获取当前sheet页的名称,当前单元格所在位置
fname = ActiveSheet.Name-------获取当前sheet页的名称 Sname = "" & fname & "&qu ...
- Spring---浅谈AOP
概念 AOP是Aspect Oriented Programming的缩写,即面向切面的编程.是一种比较新颖的编程思想,也是Spring框架中一个重要的领域. AOP将应用系统分为两个部分:核心业务逻 ...
- luogu3810 【模板】三维偏序(陌上花开)
ref1 ref2 ref3 ref4 #include <algorithm> #include <iostream> #include <cstdio> usi ...
- sc.exe
sc.exe 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 服务管理程序:可用sc.exe远程创建 外文名 sc.exe 停止事件服务 sc stop eventl ...