题意:

  有一个矩阵,每行都有序,每行接在上一行尾后仍然有序。在此矩阵中查找是否存在某个数target。

思路:

  这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了。由于用vector装的,但是也是满足线性的。

  二分:O(log n*m)

 class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
int n=matrix.size(), m=matrix[].size();
int L=, R=n*m-;
while(L<R)
{
int mid=L+(R-L+)/;
if(matrix[mid/m][mid%m]<=target) L=mid;
else R=mid-;
}
return matrix[L/m][L%m]==target;
}
};

AC代码

  

  迭代:O(n+m)

 class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
int n=matrix.size(), m=matrix[].size();
int i=, j=m-;
while(i<n && j>=)
{
if(target==matrix[i][j]) return true;
if(target>matrix[i][j]) i++;
else j--;
}
return false;
}
};

AC代码

LeetCode Search a 2D Matrix(二分查找)的更多相关文章

  1. 74. Search a 2D Matrix(二分查找,剑指offer 1)

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

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

  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 II

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

  5. 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 ...

  6. 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 ...

  7. [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找

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

  8. leetcode——Search a 2D Matrix 二维有序数组查找(AC)

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

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

  10. [leetcode]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

随机推荐

  1. BZOJ1570 [JSOI2008]Blue Mary的旅行

    建分层图,每一层表示一天的情况 从S向第0层的1号点连边,每层的n向T连INF的边 枚举天数,每多一天就多建一层然后跑最大流,如果当前流量大于人数则输出答案 由于路径长度不会超过n,因此tot个人走这 ...

  2. JAVA-数据库连接【转】

    SqlServer.Oracle.MySQL的连接,除了地址和驱动包不同,其他都一样的. 1 public String urlString="jdbc:sqlserver://localh ...

  3. 转:Linux 安装 Mysql

    前段时间安装了Mysql,但是有些问题,就想把他卸载了,重新安装一个,但是没想到在Linux卸载软件是一个很痛苦的事情.   我的Mysql是用命令的方式安装的,就是上一篇文章用到的那个命令(sudo ...

  4. 注册并启动 Reporting Services SharePoint 服务

    在安装 SharePoint 之前已安装 Reporting Services SharePoint 模式.所以Reporting Services SharePoint 是不能正常使用的. 安装完S ...

  5. linux下不能使用shutdown命令

    命令查看:  #echo $PATH     /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/sbin;/ ...

  6. 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器

    Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...

  7. Linux 服务器安全技巧

    毋庸置疑,对于系统管理员,提高服务器的安全性是最重要的事情之一.因此,也就有了许多针对这个话题而生的文章.博客和论坛帖子. 一台服务器由大量功能各异的部件组成,这一点使得很难根据每个人的需求去提供定制 ...

  8. xml装php数组

    $data = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA); $arr = converArray($data); ...

  9. JSONObject与JSONArray的使用

    1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...

  10. jQuery 中的 Ajax

    jQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法时 $.ajax(), 第二层是 load(), $.get() 和 $.post(), 第三层是 $.getScript ...