1.

设查找的数位y,第一行最后一列的数位x

如果x<y,x是第一行最大的,所以第一行都小于y,删除第一行;

如果x>y,x是最后一列最小的,所以最后一列都大于y,删除最后一列;

这样保证x永远在可能有解的矩阵的第一行,最后一列。

时间复杂度:O(m+n)

 1 class Solution {
 2 public:
 3     /**
 4      * @param matrix, a list of lists of integers
 5      * @param target, an integer
 6      * @return a boolean, indicate whether matrix contains target
 7      */
 8     bool searchMatrix(vector<vector<int> > &matrix, int target) {
 9         // write your code here
10         int m = matrix.size();
11         if (m == 0) {
12             return false;
13         }
14         int n = matrix[0].size();
15         int r = 0, c = n - 1;
16         while (r < m && c >= 0) {
17             if (matrix[r][c] < target) {
18                 r++;
19             } else if (matrix[r][c] > target) {
20                 c--;
21             } else {
22                 return true;
23             }
24         }
25         return false;
26     }
27 };

2. 分治法1

设查找的数位y,取中心点x,把矩阵分解成4部分

如果x<y,x是A中最大的,所以A都小于y,删除A;

如果x>y,x是D中最小的,所以D都小于y,删除D;

A  | B

————

C  | D

时间复杂度:O(N) = O(N/4)+O(N/2)+O(1), 介于O(N^0.5)~O(N)之间

 1 class Solution {
 2 public:
 3     /**
 4      * @param matrix, a list of lists of integers
 5      * @param target, an integer
 6      * @return a boolean, indicate whether matrix contains target
 7      */
 8     bool helper(vector<vector<int> > &M, int x1, int y1, int x2, int y2, int target) {
 9         if (x1 > x2 || y1 > y2) {//empty matrix
10             return false;
11         }
12         int midx = (x1 + x2)>>1;
13         int midy = (y1 + y2)>>1;
14         if (M[midx][midy] == target) {
15             return true;
16         }
17         return (M[midx][midy] > target)?
18         (helper(M, x1, y1, x2, midy-1, target)||helper(M, x1, midy, midx-1, y2, target)):
19         (helper(M, x1, midy+1, x2, y2, target)||helper(M, midx+1, y1, x2, midy, target));
20
21     }
22     bool searchMatrix(vector<vector<int> > &matrix, int target) {
23         // write your code here
24         int m = matrix.size();
25         if (m == 0) {
26             return false;
27         }
28         int n = matrix[0].size();
29         return helper(matrix, 0, 0, m - 1, n - 1, target);
30     }
31 };

3. 分治法2

设查找的数为y,在中线找到这样两个数x1,x2,使得x1<y,x2>y,把矩阵分成4部分

A|     B

————

C|    D

x1是A中最大的,所以A都小于y,删掉A;

x2是D中最小的,所以D都大于y,删掉D;

时间复杂度:O(N)=2O(N/4)+O(logn), 为O(N^0.5)

 1 class Solution {
 2 public:
 3     /**
 4      * @param A, a list of integers
 5      * @param left, an integer
 6      * @param right, an integer
 7      * @param target, an integer
 8      * @return an integer, indicate the index of the last number less than or equal to target in A
 9      */
10     int binarySearch(vector<int> &A, int left, int right, int target) {
11         while (left <= right) {//not an empty list
12             int mid = (left + right) >> 1;
13             if (A[mid] <= target) {
14                 left = mid + 1;//left is in the integer after the last integer less than or equal to target
15             } else {
16                 right = mid - 1;
17             }
18         }
19         return left - 1;
20     }
21     /**
22      * @param M, a list of lists of integers
23      * @param x1, an integer
24      * @param y1, an integer
25      * @param x2, an integer
26      * @param y2, an integer
27      * @param target, an integer
28      * @return a boolean, indicate whether matrix contains target
29      */
30     bool helper(vector<vector<int> > &M, int x1, int y1, int x2, int y2, int target) {
31         if (x1 > x2 || y1 > y2) {//empty matrix
32             return false;
33         }
34         int midx = (x1 + x2)>>1;
35         int indy = binarySearch(M[midx], y1, y2, target);
36         //M[midx][indy] <= target
37         if ((indy >= y1) && (M[midx][indy] == target)) {
38             return true;
39         }
40         return (helper(M, x1, indy+1, midx-1, y2, target))||(helper(M, midx+1, y1, x2, indy, target));
41
42     }
43     /**
44      * @param matrix, a list of lists of integers
45      * @param target, an integer
46      * @return a boolean, indicate whether matrix contains target
47      */
48     bool searchMatrix(vector<vector<int> > &matrix, int target) {
49         // write your code here
50         int m = matrix.size();
51         if (m == 0) {
52             return false;
53         }
54         int n = matrix[0].size();
55         return helper(matrix, 0, 0, m - 1, n - 1, target);
56     }
57 };

LintCode: Search A 2d Matrix相关推荐

  1. 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...

  2. LeetCode 240. Search a 2D Matrix II

    LeetCode 240. Search a 2D Matrix II Solution1: 为什么把第74题的代码改都不用改的拿过来就可以AC,一脸懵逼啊... class Solution { p ...

  3. LeetCode 74. Search a 2D Matrix

    LeetCode 74. Search a 2D Matrix Solution1:我的答案 <剑指offer>原题 class Solution { public:bool search ...

  4. leetcode 74 java_【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 ...

  5. LeetCode: Search a 2D Matrix

    少数次过 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int> > &matrix, ...

  6. LeetCode Search a 2D Matrix II

    问题:给出一个二维数组,其行,列是递增的,和一个要查找的数,问矩阵中是否有要查找的数 思路: 第一种方法是直接暴力查找,在二维数组中查找. 第二种方法是基于对角线上,在行,列上作二分查找 第三种方法递 ...

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

  8. 74. Search a 2D Matrix (Graph; Divide-and-Conquer)

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

  9. leetcode 240. Search a 2D Matrix II | 240. 搜索二维矩阵 II(Java)

    题目 https://leetcode.com/problems/search-a-2d-matrix-ii/ 题解 方法1 思路类似于 leetcode 200. Number of Islands ...

最新文章

  1. beeline执行sql语句_由“Beeline连接HiveServer2后如何使用指定的队列(Yarn)运行Hive SQL语句”引发的一系列思考...
  2. Java1.5语言新特性简单总结
  3. 【组合数学】组合恒等式 ( 递推 组合恒等式 | 变下项求和 组合恒等式 简单和 | 变下项求和 组合恒等式 交错和 )
  4. python前端开发之准备开发环境(建议收藏)
  5. ffmpeg 源码学习之seek play
  6. 解决VS2019窗体设计器不显示
  7. Dapr + .NET 实战(十三)跨语言开发
  8. JSF:直接从页面将参数传递给JSF操作方法,这是JavaEE 6+的一个不错的功能
  9. EasyExcel快速上手~读取
  10. jquery学习笔记四:ajax
  11. Asp.net页面之间传递参数的几种方法荟萃
  12. (10)Spring框架----AOP面向切面编程的实现原理
  13. TPLink路由器登陆密码怎么破解
  14. Word导出pdf时Origin图片出现重影(重叠)
  15. 简单的w7-->w10的方法
  16. 【序列化】Kryo 的几种常见序列化实现方式,及其兼容性
  17. 磁盘容量超过64T分配单元大小需要设置64K
  18. USB转多路串口 USB hub USB扩展
  19. TQ2440 学习笔记—— 3、如何在ubuntu 9.10 下安装vmware-tools?
  20. OneNav一为主题魔改教程(四):自定义网址内容页的Tag标签到任意位置--洞五洞洞幺

热门文章

  1. C++(11)--编程实践1-经典养成类游戏简单实践
  2. if __name__ == __main___python中 __name__ == #x27;__main__#x27; 有什么作用?
  3. 中医教你5个补肾护发食疗方
  4. linux opencl(AMD) Example
  5. 最易忽视的肾虚4件事
  6. 个人理财有哪些基本原理和方法?
  7. inline 内联函数详解 内联函数与宏定义的区别
  8. VC画图用到的主要方法
  9. IT人的好习惯和不良习惯总结
  10. 只用一套解决方案,就可解决80%的交通物流行业信息难题