Search a 2D Matrix II

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 in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

For example,

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

递归,每层排除中心元素左上角或者右下角的1/4

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty() || matrix[].empty())
return false;
int m = matrix.size();
int n = matrix[].size();
return Helper(matrix, , m-, , n-, target);
}
bool Helper(vector<vector<int>>& matrix, int rlow, int rhigh, int clow, int chigh, int target)
{
if(rlow > rhigh || clow > chigh)
return false;
if(rlow == rhigh && clow == chigh)
return matrix[rlow][clow] == target;
int rmid = (rlow + rhigh) / ;
int cmid = (clow + chigh) / ;
int val = matrix[rmid][cmid];
if(val == target)
return true;
else if(val > target)
return Helper(matrix, rlow, rmid-, clow, chigh, target)
|| Helper(matrix, rmid, rhigh, clow, cmid-, target);
else
return Helper(matrix, rmid+, rhigh, clow, chigh, target)
|| Helper(matrix, rlow, rmid, cmid+, chigh, target);
}
};

【LeetCode】240. Search a 2D Matrix II的更多相关文章

  1. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【刷题-LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  3. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  4. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  5. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

  6. 【Lintcode】038.Search a 2D Matrix II

    题目: Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence ...

  7. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

随机推荐

  1. JS高级 - 面向对象5(继承,引用)

    <script type="text/javascript"> //------------------Person类 //(Person)的构造函数 function ...

  2. BZOJ 3771 Triple FFT+容斥原理

    解析: 这东西其实就是指数型母函数? 所以刚开始读入的值我们都把它前面的系数置为1. 然后其实就是个多项式乘法了. 最大范围显然是读入的值中的最大值乘三,对于本题的话是12W? 用FFT优化的话,达到 ...

  3. free -m图解

  4. TNS:listener does not currently know of service requested in connect descriptor错误改正

    (SID_LIST = (SID_DESC =  (SID_NAME = PLSExtProc)  (ORACLE_HOME = E:\oracle\product\10.2.0\db_1)  (PR ...

  5. request.environ.get('wsgi.websocket')

    前言 websocket 是一种html5新的接口,以前服务器推送需要进行ajax等方式进行轮训,对服务器压力较高,随着新标准的推进,使用websocket在推送等方面已经是比较成熟了,并且各个浏览器 ...

  6. AGC 018E.Sightseeing Plan(组合 DP)

    题目链接 \(Description\) 给定三个不相交的矩形\(A(X1,Y1)-(X2,Y2),B(X3,Y3)-(X4,Y4),C(X5,Y5)-(X6,Y6)\),求 从第一个矩形中某点\(a ...

  7. BZOJ.4566.[HAOI2016]找相同字符(后缀数组 单调栈)

    题目链接 给定两个字符串,求它们有多少个相同子串.相同串的位置不同算多个. POJ3145简化版. 后缀自动机做法见这儿,又快又好写(一下就看出差距了..) //13712kb 4076ms #inc ...

  8. POJ.2750.Potted Flower(线段树 最大环状子段和)

    题目链接 /* 13904K 532ms 最大 环状 子段和有两种情况,比如对于a1,a2,a3,a4,a5 一是两个端点都取,如a4,a5,a1,a2,那就是所有数的和减去不选的,即可以计算总和减最 ...

  9. 2007 ACM 平方和立方和

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2007 注意审题就好,x可以>y; #include<stdio.h> int main( ...

  10. windows + php + redis的安装

    我讲述一下我在 php 中安装 redis 的详细过程,仅供参考: 系统版本:windows7 + 64 位操作系统. php版本 : php5.6 redis版本 : redis 2.2.7 (由于 ...