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. 第一个Servlet程序及分析

    第一个Servlet程序: package cc.openhome; import java.io.IOException; import java.io.PrintWriter; import ja ...

  2. Spark算子总结及案例

    spark算子大致上可分三大类算子: 1.Value数据类型的Transformation算子,这种变换不触发提交作业,针对处理的数据项是Value型的数据. 2.Key-Value数据类型的Tran ...

  3. Spring MVC整合DWR

    http://blog.csdn.net/geloin/article/details/7537148 基本上与上文描述的情况一致: 在Controller中可以进行Service层的调用: 如果需要 ...

  4. ORACLE的Dead Connection Detection浅析

    在复杂的应用环境下,我们经常会遇到一些非常复杂并且有意思的问题,例如,我们会遇到网络异常(网络掉包.无线网络断线).客户端程序异常(例如应用程序崩溃Crash).操作系统蓝屏.客户端电脑掉电.死机重启 ...

  5. CodeForces758D

    D. Ability To Convert time limit per test:1 second memory limit per test:256 megabytes input:standar ...

  6. jq基础

    $(function() {          $(".dd").attr("class","cc").append("<h ...

  7. Unsupported major.minor version 52.0错误解决 Ubuntu JDK8 安装配置

    Unsupported major.minor version 52.0错误一般是因为应用程序需要JDK8而ubuntu默认的是jdk7,所以需要切换到jdk8才能解决这个问题. 本文使用PPA方式安 ...

  8. C++源码的调用图生成

    前言 之前受知乎用户mailto1587启发,写了个C++源码的调用图生成器,可以以图示法显示C++函数的调用关系, 代码放在了github仓库里,仅供参考: CodeSnippet/python/S ...

  9. matlab 中max函数用法

    Matlab中max函数在矩阵中求函数大小的实例如下:(1)C = max(A)返回一个数组各不同维中的最大元素.如果A是一个向量,max(A)返回A中的最大元素.如果A是一个矩阵,max(A)将A的 ...

  10. nodejs笔记1 ----关于express不是本地命令

    1,npm install -g express安装express框架 2,npm install -g express-generator安装命令工具 3,express --help 安装完成