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. [
  2. [1, 3, 5, 7],
  3. [10, 11, 16, 20],
  4. [23, 30, 34, 50]
  5. ]

Given target = 3, return true.

假设直接对矩阵元素进行二分查找的话,时间复杂度是O(m*n),事实上非常easy想到先通过查找找到相应可能存在于哪一行,然后再在那行中查找是否存在,採用最简单的直接查找这样时间复杂度仅有O(m+n),假设这两次查找再分别採用二分查找的话,时间复杂度更能够减少到O(logm+logn),以下是O(m+n)的代码:

  1. class Solution {
  2. public:
  3. bool searchMatrix(vector<vector<int> > &matrix, int target) {
  4. if(matrix.empty())
  5. return false;
  6. int m = matrix.size();
  7. int n = matrix[0].size();
  8. int i = 0, j=0;
  9. while(i<m && target>=matrix[i][0])
  10. i++;
  11. i--;
  12. if(i==-1)
  13. return false;
  14. while(j<n)
  15. {
  16. if(target == matrix[i][j])
  17. return true;
  18. else
  19. j++;
  20. }
  21. return false;
  22. }
  23. };

leetcode——Search a 2D Matrix 二维有序数组查找(AC)的更多相关文章

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

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

  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 II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

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

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

  8. LeetCode Search a 2D Matrix(二分查找)

    题意: 有一个矩阵,每行都有序,每行接在上一行尾后仍然有序.在此矩阵中查找是否存在某个数target. 思路: 这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了.由于用vector ...

  9. [LeetCode] Search a 2D Matrix [25]

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

随机推荐

  1. 解决Jetty Maven Plugin:Please initialize the log4j system properly(转)

    解决Jetty Maven Plugin:Please initialize the log4j system properly.Jetty Maven Plugin环境: <plugin> ...

  2. 转:STL使用入门( Using STL)

    1 介绍 我最开始结束C++编程是从DOS下的Borland C++开始的.那时他们在最新版本3.1中就包含了一套模板库用来做collection.那真是个好东东.当我开始使用Visual C++ 2 ...

  3. 基于visual Studio2013解决C语言竞赛题之0409 100以内素数

       题目 解决代码及点评 在已经知道素数是怎么判断的基础上,增加循环,可以判断出100以内的素数 /******************************************* ...

  4. (3)选择元素——(4)css选择器(CSS selectors)

    The jQuery library supports nearly all of the selectors included in CSS specifications 1 through 3, ...

  5. FMDB的基本应用

    FMDB简介 iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较频繁.于是,就出现了一系列将AQLite API进行封装的库,例如FMDB.PlausibleDa ...

  6. jbpm系列之五--使用decision节点判断分支情况

    我们在用JBPM做流程的时候,很多时候会遇到需要判断的节点.类似java中的switch,根据不同的状态,跳转到不同的节点. 首先我们定义一个流程信息,jpdl流程图如下 明显的可以看到,在此种情况下 ...

  7. 怎么在Ubuntu Scope中获取location地址信息

    Location信息对非常多有地址进行搜索的应用来说非常重要.比方对dianping这种应用来说.我们能够通过地址来获取当前位置的一些信息.在这篇文章中,我们来介绍怎样获取Scope架构中的位置信息. ...

  8. Web API 设计摘要

    近期读了一本微电子书 Brian Mulloy 所著<Web API Design>感觉颇多收获,特对其内容做了个整理摘要以便回想其观点精华以指导日常工作中的设计思路. 本文主要讲述 We ...

  9. git 使用过程(三、文件的添加 修改)

    1.库中添加文件 在目录下新建一个文件 如 testfile.txt .输入命令:① git add testfile.txt  ②git commit -m "这里是你提交的说明" ...

  10. ThinkPHP快捷函数

    16个快捷函数用法 1.A() 实例化控制器  格式:[资源://][模块/]控制器A($name,$layer='',$level='')@param string $name 资源地址   @pa ...