问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找。

算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法。

  1. public class SearchInSortedMatrix
  2. {
  3. public static boolean searchMatrix(int[][] matrix, int target)
  4. {
  5. int m = matrix.length;
  6. int n = matrix[0].length;
  7. int low = 0;
  8. int high = m*n - 1;
  9. while(low <= high)
  10. {
  11. int mid = (low + high)/2;
  12. int row = mid / n;
  13. int column = mid % n;
  14. if(matrix[row][column] == target)
  15. {
  16. return true;
  17. }
  18. else if(matrix[row][column] < target)
  19. {
  20. low = mid + 1;
  21. }
  22. else
  23. {
  24. high = mid - 1;
  25. }
  26. }
  27. return false;
  28. }
  29. }

问题描述:二维矩阵行有序,列有序,进行查找。

算法分析:有两种方法,一种是将矩阵按中心点分成左上,左下,右上,右下,四部分,进行递归查找。

     还有一种比较巧妙的查找方法,就是从左下角或者右上角的元素进行查找。

  1. //矩阵每一行有序,每一列有序
  2. public boolean searchMatrix2(int[][] matrix, int target)
  3. {
  4. int m = matrix.length;
  5. int n = matrix[0].length;
  6. return helper(matrix, 0, m-1, 0, n-1, target);
  7. }
  8. public boolean helper(int[][] matrix, int rowStart, int rowEnd, int colStart, int colEnd, int target)
  9. {
  10. int rm = (rowStart + rowEnd)/2;
  11. int cm = (colStart + colEnd)/2;
  12. if(rowStart > rowEnd || colStart > colEnd)
  13. {
  14. return false;
  15. }
  16. if(matrix[rm][cm] == target)
  17. {
  18. return true;
  19. }
  20. else if(matrix[rm][cm] > target)
  21. {
  22. return helper(matrix, rowStart, rm - 1, colStart, cm - 1, target)
  23. || helper(matrix, rm, rowEnd, colStart, cm - 1, target)
  24. || helper(matrix, rowStart, rm - 1, cm, colEnd, target);
  25. }
  26. else
  27. {
  28. return helper(matrix, rm + 1, rowEnd, cm + 1, colEnd, target)
  29. || helper(matrix, rm + 1, rowEnd, colStart, cm, target)
  30. || helper(matrix, rowStart, rm, cm + 1, colEnd, target);
  31.  
  32. }
  33. }
  34.  
  35. //从右上角元素进行查找
  36. public boolean searchMatrix3(int[][] matrix, int target)
  37. {
  38. int m = matrix.length;
  39. int n = matrix[0].length;
  40. int low = 0;
  41. int high = m*n - 1;
  42. while(low <= high)
  43. {
  44. int mid = (low + high)/2;
  45. int row = mid / n;
  46. int column = mid % n;
  47. if(matrix[row][column] == target)
  48. {
  49. return true;
  50. }
  51. else if(matrix[row][column] < target)
  52. {
  53. low = mid + 1;
  54. }
  55. else
  56. {
  57. high = mid - 1;
  58. }
  59. }
  60. return false;
  61. }

Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。的更多相关文章

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

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

  5. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

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

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

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

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

  8. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

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

  9. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

  10. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

随机推荐

  1. 第二课作业——redis常用命令

    第二课时作业 静哥 by 2016.2.23~2016.2.22   [作业描述] 1.key string list hash结构中,每个至少完成5个命令,包含插入 修改 删除 查询,list 和h ...

  2. Web 编程中路径问题

    web.xml 中 <url-pattern> 路径(即 Servlet 路径) 要么以 "*" 开头, 要么以 "/" 开头. 转发和包含路径(服 ...

  3. cookies设置时间

    默认cookies失效时间是直到关闭浏览器,cookies失效,也可以指定cookies时间. Response.Cookies("user_name").Expires=Date ...

  4. 关于session的常用用法

    (一)django有四中session实现方式 1.数据库(database-backed sessions) 2.缓存(cached sessions) 3.文件系统(file-based sess ...

  5. TouchDelegate

    TouchDelegate(Rect bounds, View delegateView) Parameters: bounds Bounds in local coordinates of the ...

  6. Android View学习Tips

    1.Canvas.save()和Canvas.restore() canvas.save();和canvas.restore();是两个相互匹配出现的,作用是用来保存画布的状态和取出保存的状态的.这里 ...

  7. 012-JDK可视化监控工具-jstack

    一.概述 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...

  8. pgadmin3

    一般性  \copyright            显示PostgreSQL的使用和发行许可条款  \g [文件] or;     执行查询 (并把结果写入文件或 |管道)  \gset [PREF ...

  9. nginx常用

    1.rewrite return 301 http://example.com$request_uri; rewrite ^ http://example.com permanent; 2.try_f ...

  10. GIL解释器,协程,gevent模块

    GIL解释器锁 在Cpython解释器中,同一个进程下开启的多线程,同一时刻只能有一个线程执行,无法利用多核优势 首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CP ...