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.
这题很简单
思路:二分法确定target可能在第几行出现。再用二分法在该行确定target可能出现的位置。时间复杂度O(logn+logm)
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m=matrix.size();
int n=matrix[].size();
int l=,r=m-,mid;
if(target>matrix[m-][n-]||target<matrix[][])
return false;
while(l<r)
{
mid=l+(r-l)/;
if(target==matrix[mid][n-])
return true;
else if(target<matrix[mid][n-])
r=mid;
else
l=mid+;
}
int index=r;
l=;
r=n-;
while(l<=r)
{
mid=l+(r-l)/;
if(target==matrix[index][mid])
return true;
else if(target<matrix[index][mid])
r=mid-;
else
l=mid+;
}
return false;
}
};
Search a 2D Matrix——两度二分查找的更多相关文章
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- LeetCode Search a 2D Matrix(二分查找)
题意: 有一个矩阵,每行都有序,每行接在上一行尾后仍然有序.在此矩阵中查找是否存在某个数target. 思路: 这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了.由于用vector ...
- [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 ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- [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 follo ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 28. Search a 2D Matrix 【easy】
28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
随机推荐
- React router 4 获取路由参数,跨页面参数
1. match通过路径 <Route path="/path/:name" component={example} /> 路由组件内获取参数使用 this.props ...
- swift的一些东西
.cmd+k 键盘toggle .模拟器的handware设置ios键盘 .设置textfield的return类型为搜索 k.returnKeyType=UIReturnKeyType.search ...
- python升级引发的问题总结
http://www.cnblogs.com/ajianbeyourself/p/4214398.html http://www.cnblogs.com/hanggegege/p/6993003.ht ...
- Qt -------- 容器类
QVector(数组).QLinkedList(链表).QMap(映射表).QHash(哈希表).QQueue(队列) QHash遍历举例: 法1: QThread& ThreadHandle ...
- ObservableCollection 类
假设您正在创建 Windows 窗体应用程序,并且已将 DataGridView 控件绑定到标准 List(Of Customer) 数据结构.您希望能够使网格中的项目与基础数据源中的值保持同步.也就 ...
- jsoup select 选择器
转载自:http://blog.csdn.net/zhejingyuan/article/details/11801027 方法 利用方法:Element.select(String selector ...
- sql cmd命令执行sqlserver的sql文件
有的时候,我们通过Log Explorer工具根据日志生成的回滚脚本,或者其他情况我们得到的脚本文件,通过sqlserver打开脚本文件的方式不爽,我们可以这样: 方式一: osql -S . -U ...
- 超酷算法-BK树
前几天无意间遇到一个博客,觉得写得挺好的,自己之前的时候有个不好的习惯,那就是遇到了好资源第一反应就是收藏起来然后却很少再看!!这是坏习惯,要改!于是今天就开始通读了,读的第二篇是BK树.觉得有点意思 ...
- WPF 添加Adminstrator 权限
在WPF应用开发中,需要WPF操作后台注册的Windows Service,可是WIX打包的安装程序不具备赋予WPF App默认管理员权限. 因此,需要我们手工在WPF项目中添加管理员权限: 1.右击 ...
- CCD和CMOS的差别
单从感光器电子技术上来说,CCD比CMOS更先进,理论成像上有优势,但是最近几年CMOS却发展更好,使得很多高端数码单反采用CMOS传感器,下面来看看CCD和CMOS的技术知识: CCD和CMOS传感 ...