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.

Subscribe to see which companies asked this question

首先二分查找每行的第一个元素,确定了行号后,再在当前行内进行二分查找

值得一提的是其时间复杂度为 O(log(m) + log(n)) = O(log(m*n))

bool searchMatrix(vector<vector<int>>& matrix, int target) {
int beg = , mid, end = matrix.size()-;
while (beg <= end)
{
mid = (beg + end) >> ;
if (target < matrix[mid][])
end = mid - ;
else if (target > matrix[mid][])
beg = mid + ;
else
return true;
}
int row = end;
if (row < )
return false;
beg = ;
end = matrix[row].size();
while (beg <= end)
{
mid = (beg + end) >> ;
if (target < matrix[row][mid])
end = mid - ;
else if (target > matrix[row][mid])
beg = mid + ;
else
return true;
}
return false;
}

Search a 2D Matrix leetcode的更多相关文章

  1. Search a 2D Matrix leetcode java

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  2. Search a 2D Matrix ——LeetCode

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. [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 ...

  4. [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 ...

  5. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  6. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  7. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

  8. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  9. [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 ...

随机推荐

  1. 【MySQL】容器集群支持数据库实践

    京东容器数据库系统,管理1800台物理计算节点,生产1W+ 多MySQL Docker容器实例.架构简单可靠,Docker容器计算平台与MySQL集群管理平台解耦处理.为描述方便,京东容器化数据库系统 ...

  2. oracle语句总结(一)

    1,建外键 新建两个表来实现外键的关联. create table main_tab ( id number, name varchar2(30) ); create table sub_tab ( ...

  3. swift 定位 根据定位到的经纬度转换城市名

    好久没写随笔了   最近这段时间项目有点紧  天天在加班  国庆 一天假都没放  我滴娃娃   好啦  牢骚就不发了  毕竟没有什么毛用    待我那天闲了专门写一篇吐槽的随笔  

  4. Spring context:component-scan代替context:annotation-config

    Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...

  5. 巧用css text-indent减小中文标点符号的占位大小

    由于设计需要,我们的页面中经常会有如下效果: 可是我们实现出来的效果确实这样的: 看起来两行文本没有对齐嘛,仔细检查后原来是[字符的原因,因为是中文标点符号占半个字的位置.不信?选中下汉字标点符号看一 ...

  6. jmeter测试计划

    测试计划配置 用户定义的变量: 测试计划上可以添加用户定义的变量.一般添加一些系统常用的配置.如果测试过程中想切换环境,切换配置,一般不建议在测试计划上添加变量,因为不方便启用和禁用,一般是直接添加用 ...

  7. Redis系列四(keepalived+lvs搭建负载均衡)

    1.安装Keepalived(主备服务器都要安装) 10.8.80.218  主服务器 10.8.80.217  备服务器 10.8.80.200  虚拟IP $ wget http://www.ke ...

  8. Java内存回收优化及配置

    原文链接:http://eol.cqu.edu.cn/eol/jpk/course/preview/jpkmaterials_folder_txtrtfview.jsp?resId=23156& ...

  9. KB奇遇记(3):IT现状

    2015年8月3号,终于告别了过去来到了KB. 公司给安排的住房是一间套房里的小房间,小的简直连坐的地方都没有了,中间一个大床将房间隔了两边,显得特别狭小.由于是刚来,我也不好要求太多.但就这个小房间 ...

  10. easyui datagrid的json格式

    easyui datagrid的json格式: {"columns":[[{"field":"one","title": ...