【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 ...
随机推荐
- poj 1961 (求字符串中的重复子串)
Sample Input 3aaa12aabaabaabaab0Sample Output Test case #12 23 3 Test case #22 2 //aa有2个a6 2 //aabaa ...
- S2750&S5700&S6700 V200R003(C00&C02&C10) MIB参考
https://support.huawei.com/enterprise/docinforeader.action?contentId=DOC1000027337&idPath=791971 ...
- python全栈开发day47-jqurey
一.昨日内容回顾 二.今日内容总结 1.jquery的介绍 1).为什么要用jquery? # window.onload 事件有事件覆盖的问题,因此只能写一个事件. # 代码容错性差 # 浏览器兼容 ...
- python全栈开发day37-html
web准备总结: 结构标准:相当于人的身体.html就是用来制作网页的. 表现标准: 相当于人的衣服.css就是对网页进行美化的. 行为标准: 相当于人的动作.JS就是让网页动起来,具有生命力的 1. ...
- Linux使用netstat命令查看并发连接数
我们的网站部署在linux的服务器上,特别是web服务器,我们可能有时候做为运维人员,肯定是要查看网站的并发连接数是不是达到瓶颈等,所以在linux下,我们如何查看服务器的并发连接数呢?使用以下命令即 ...
- zyb的面试
今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?例如n=15,k=7, 排列顺序 ...
- Python - 常用更新命令以及常见库安装
库的安装方式一般有两种: 一. pip直接安装(或使用豆瓣源) pip install scrapy pip install -i https://pypi.douban.com/simple/ sc ...
- word 中如何取消格式标记
开始菜单栏下: 或者:
- maven环境的配置,如果jar包下载不下来,其他配置无错误的话,极有可能是网速的缘故
1首先下载apach maven 2配置maven环境变量 m2_home maven的源文件的路径 path变量后跟 %m2_home%\bin 3cmd 控制台运行mvn -version 查看 ...
- Redis分布式锁实现方式(附有正解及错误示例)
一.前言 本文内容主要来自博客:https://wudashan.com/2017/10/23/Redis-Distributed-Lock-Implement/,本文用于归纳总结及笔记用途,如有需要 ...