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, returntrue.

题意:在一个二维矩阵中,查询一个数是否存在。数组:1)每行从左到右从下到大排好;2)行首元素大于上一行的最后一个元素;

思路:常规思路:先遍历行找到元素所可能在的行,然后遍历列,判断是否在在该行中,时间复杂度O(n+m);二分查找版本一:是对常规思路的升级,先查找行 ,再查找列,但这时使用的查找的方法不是从头到尾的遍历,是二分查找,值得注意的是查找完行以后的返回值,时间复杂度O{logn+logm)二分查找版本二:因为矩阵数排列的特性,可以看成一个排列好的一维数组[0, n*m],可以针对整个二维矩阵进行二分查找,时间复杂还是O(log(n*m)),这里的难点是,如何将二维数组的下标和一维数组之间进行转换。

方法一:

  1. class Solution {
  2. public:
  3. bool searchMatrix(vector<vector<int> > &matrix, int target)
  4. {
  5. int row = matrix.size();
  6. int col = matrix[].size();
  7. int subRow = ;
  8. if (row == || col == ) return false;
  9.  
  10. //寻找行
  11. if (matrix[row - ][] <= target) //最后一行,特殊处理
  12. subRow = row - ;
  13. else
  14. {
  15. for (int i = ; i<row - ; ++i)
  16. {
  17.  
  18. if ((matrix[i][] <= target) && (matrix[i + ][]>target))
  19. {
  20. subRow = i;
  21. break;
  22. }
  23. }
  24. }
  25.  
  26. //查找列
  27. for (int j = ; j<col; ++j)
  28. {
  29. if (matrix[subRow][j] == target)
  30. return true;
  31. }
  32. return false;
  33. }
  34. };

方法二:如下:

  1. // Two binary search
  2. class Solution {
  3. public:
  4. bool searchMatrix(vector<vector<int> > &matrix, int target)
  5. {
  6. int row=matrix.size();
  7. int col=matrix[].size();
  8. if (row== || col==) return false;
  9. if (target < matrix[][] || target > matrix[row-][col-]) return false;
  10.  
  11. //查找行
  12. int lo = , hi = row - ;
  13. while (lo <= hi)
  14. {
  15. int mid = (lo+hi) / ;
  16. if (matrix[mid][] == target)
  17. return true;
  18. else if (matrix[mid][] < target)
  19. lo = mid + ;
  20. else
  21. hi = mid - ;
  22. }
  23. int tmp = hi; //特别注意
  24. //查找该行
  25. lo = ;
  26. hi = col - ;
  27. while (lo <= hi)
  28. {
  29. int mid = (lo+hi) / ;
  30. if (matrix[tmp][mid] == target)
  31. return true;
  32. else if (matrix[tmp][mid] < target)
  33. lo = mid + ;
  34. else
  35. hi = mid - ;
  36. }
  37. return false;
  38. }
  39. };

方法三:

  1. class Solution {
  2. public:
  3. bool searchMatrix(vector<vector<int> > &matrix, int target)
  4. {
  5. int row = matrix.size();
  6. int col = matrix[].size();
  7.  
  8. if(row==||col==) return false;
  9. if(matrix[][]>target||target>matrix[row-][col-]) return false; //加与不加都行
  10.  
  11. int lo=,hi=row*col-;
  12. while(lo<=hi)
  13. {
  14. int mid=(lo+hi)/;
  15. int i=mid/col;
  16. int j=mid%col;
  17. if(target==matrix[i][j])
  18. return true;
  19. else if(target>matrix[i][j])
  20. lo=mid+;
  21. else
  22. hi=mid-;
  23. }
  24. return false;
  25. }
  26. };

[Leetcode] search a 2d matrix 搜索二维矩阵的更多相关文章

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

  2. 074 Search a 2D Matrix 搜索二维矩阵

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行中的整数从左到右排序.    每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[  [1,   3, ...

  3. Leetcode74. Search a 2D Matrix搜索二维矩阵

    编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...

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

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

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

  6. LeetCode(74):搜索二维矩阵

    Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  7. 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 OJ:Search a 2D Matrix(二维数组查找)

    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 II 搜索一个二维矩阵之二

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

随机推荐

  1. 解决url传递过程中加号变空格的问题

    url传递过程中加号变空格 在接收url参数的过程中,会发现如果参数中存在‘+’号,接收后会变成空格. 如11+22接收后变成11 22. 要解决这个问题,需要将加号替换为%2B进行传递. 如11%2 ...

  2. mysql 优化like查询

    1. like %keyword    索引失效,使用全表扫描.但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描. 2. like keyword%    索引有 ...

  3. plsql 连接数据库无法解析指定的连接标识符

    之前用plsql连接的时候一直出问题,报无法解析指定的连接标识符,但是我加上ip地址就可以连接上. 我百度了很久,有说如下图选择oracle home的,有说清空admin目录下的所有文件, 但是都不 ...

  4. Linux 内核之api_man 手册安装

    开发环境:Ubuntu18.04,虚拟机virtual box 1.安装XML格式转换 sudo apt  install xmlto 2.在内核目录执行 make mandocs  大概持续了半小时 ...

  5. pyspider -- 禁止请求非200响应码抛异常

    在pyspider中若crawl()网址时出现非200的异常信息,会抛出一个异常. 可以在对应的回调函数上面通过@catch_status_code_error 进行修饰,这样就能不抛出异常正常进入回 ...

  6. Preparing Cities for Robot Cars【城市准备迎接自动驾驶汽车】

    Preparing Cities for Robot Cars The possibility of self-driving robot cars has often seemed like a f ...

  7. go学习笔记-运算符

    运算符 运算符 内置运算符 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 算术运算符 假定 A 值为 10,B 值为 20. 运算符 描述 实例 + 相加 A + B 输出结果 ...

  8. TCD产品技术参考资料

    1.Willis环 https://en.wikipedia.org/wiki/Circle_of_Willis 2.TCD仿真软件 http://www.transcranial.com/index ...

  9. HDU暑假多校第四场J-Let Sudoku Rotate

    一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...

  10. 3,jieba gensim 最好别分家之最简单的相似度实现

    简单的问答已经实现了,那么问题也跟着出现了,我不能确定问题一定是"你叫什么名字",也有可能是"你是谁","你叫啥"之类的,这就引出了人工智能 ...