74. Search a 2D Matrix (Graph; Divide-and-Conquer)
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
.
思路:先按行二分搜索得到行号,再按列二分搜索
class Solution { public:bool searchMatrix(vector<vector<int>>& matrix, int target) {int start = 0, end = matrix.size()-1;int mid;int lineNum;while(start<=end){mid = start + ((end-start)>>1);if(matrix[mid][0]<target){start = mid+1;}else if(matrix[mid][0]>target){end = mid-1;}else return true;}if(end < 0) return false;lineNum = end;start = 0;end = matrix[0].size()-1;while(start<=end){mid = start + ((end-start)>>1);if(matrix[lineNum][mid]<target){start = mid+1;}else if(matrix[lineNum][mid]>target){end = mid-1;}else return true;}return false;} };
转载于:https://www.cnblogs.com/qionglouyuyu/p/4854625.html
74. Search a 2D Matrix (Graph; Divide-and-Conquer)相关推荐
- 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...
- LeetCode 74. Search a 2D Matrix
LeetCode 74. Search a 2D Matrix Solution1:我的答案 <剑指offer>原题 class Solution { public:bool search ...
- leetcode 74 java_【LeetCode】74. Search a 2D Matrix
问题描述 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...
- Array | 74. Search a 2D Matrix
题目:搜索二维矩阵 方法1: class Solution { public:bool searchMatrix(vector<vector<int>>& matrix ...
- 74. Search a 2D Matrix
文章目录 1 题目理解 2 二分 1 题目理解 输入:一个mxn的int数组matrix,这个数组每一行按照从小到大排序,并且下一行的第一个值大于上一行的最后一个值.一个int值target 返回:t ...
- [Leetcode] 74. Search a 2D Matrix 解题报告
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- LeetCode 240. Search a 2D Matrix II
LeetCode 240. Search a 2D Matrix II Solution1: 为什么把第74题的代码改都不用改的拿过来就可以AC,一脸懵逼啊... class Solution { p ...
- LeetCode 74. Search a 2D Matrix--有序矩阵查找--python,java,c++解法
题目地址:Search a 2D Matrix - LeetCode Write an efficient algorithm that searches for a value in an m x ...
- [Swift]LeetCode74. 搜索二维矩阵 | Search a 2D Matrix
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
最新文章
- “网络实名制” 你认为有必要吗?
- oracle经典书籍推荐
- 笔记整理-信息系统开发基础-软件测试-模糊测试
- 日志挖掘(logminer)
- java edittext 输入监听_Android应用开发之Android EditText 监听用户输入完成的实例
- OpenKG 祝大家新春快乐
- Apsara Stack 技术百科 | 边缘场景智能云化,让云无处不在
- android webview recyclerview,文章详情页的实现——WebView和RecyclerView混合
- 多重共线性问题的几种解决方法
- mysql 锁 行级_全局锁、表级锁、行级锁mysql 极客评论笔记
- how-to-change-the-windows-pagefile-size win10分页虚存大小
- 初学cesium时的一些笔记,过于潦草看看就好
- 解决Linux终端无法复制粘贴现象
- 计算24点游戏C语言课设
- java tic tac toe_确定Tic Tac Toe游戏的算法
- 圆圆的球像什么_圆圆的月亮像什么-圆圆的月亮比喻句
- linux 中 man 命令的介绍
- 计算机应用知识试题 上学吧,自考计算机应用高分技巧,自考计算机应用作答技巧...
- verilog状态机的三种写法
- UDT的连接建立和释放
热门文章
- jQuery scroll事件
- 【Node.js】编码实现目录浏览服务
- uniapp开发微信小程序时,报错:Now you can provide attr `wx:key` for a `wx:for` to improve performance
- linux如何给各节点时间同步,Linux 系统配置各个节点的时间一致性
- ad怎么批量改元器件封装_Altium Designer 批量修改元件封装的方法(修正)
- php的功能和特点,php有什么特点
- 遍历所有点的最短路径python_所有节点最短路径
- 实现弹出悬浮页面_30秒快速实现Excel每页标题行的打印!
- java获取其他类的线程,使用Java实现面向对象编程——第七章 多线程
- 动态壁纸安卓_安卓 高清 动态 壁纸