【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. 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的更多相关文章
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【刷题-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 ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- 【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 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [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 ...
- 【一天一道LeetCode】#74. Search a 2D Matrix
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
随机推荐
- 微信小程序~wx.getUserInfo逐渐废弃,小程序登录过程将如何优化?
很多的时候我们在做小程序应用的时候,希望用户在使用小程序前进行登录授权,之前登录后通过wx.getUserInfo直接弹出授权的登录方式官方的意思是将不再支持,而是让用户通过下面的方式授权用户信息 & ...
- URAL - 1078 Segments
URAL - 1078 题目大意:有n条线段,一个线段a 完全覆盖另一个线段b 当且仅当,a.l < b.l && a.r>b.r.问你 一个线段覆盖一个线段再覆盖一个线段 ...
- 【Java】 剑指offer(40) 最小的k个数
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入n个整数,找出其中最小的k个数.例如输入4.5.1.6.2.7 ...
- [OpenCV-Python] OpenCV 中的图像处理 部分 IV (五)
部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 22 直方图 22.1 直方图的计算,绘制与分析目标 • 使用 OpenCV 或 Numpy 函数计算直方图 • ...
- UVa140 Bandwidth 【最优性剪枝】
题目链接:https://vjudge.net/contest/210334#problem/F 转载于:https://www.cnblogs.com/luruiyuan/p/5847706.ht ...
- 谁记录了mysql error log中的超长信息(记pt-stalk一个bug的定位过程)
[问题] 最近查看MySQL的error log文件时,发现有很多服务器的文件中有大量的如下日志,内容很长(大小在200K左右),从记录的内容看,并没有明显的异常信息. 有一台测试服务器也有类似的问题 ...
- 用redis来实现Session保存的一个简单Demo
现在很多项目都用Redis(RedisSessionStateProvider)来保存Session数据,但是最近遇到一个比较典型的情况,需要把用户数据全部load到redis里面,在加上RedisS ...
- 解决binwalk运行提示缺少LZMA模块
解决binwalk运行提示缺少LZMA模块 部分用户在使用binwalk提取固件内容,可能会遇到缺少LZMA模块的提示.提示内容为WARNING:The Python LZMA module co ...
- CRC类(处理ITU表)
class Crc { // CRC-ITU查找表 private static UInt16[] crctab16 = new UInt16[] { 0x0000, 0x1189, 0x2312, ...
- 51Nod1577 异或凑数 线性基 构造
国际惯例的题面:异或凑出一个数,显然是线性基了.显然我们能把区间[l,r]的数全都扔进一个线性基,然后试着插入w,如果能插入,则说明w不能被这些数线性表出,那么就要输出"NO"了. ...