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. SpringBank 开发日志 一种简单的拦截器设计实现

    当交易由Action进入Service之前,需要根据不同的Service实际负责业务的不同,真正执行Service的业务逻辑之前,做一些检查工作.这样的拦截器应该是基于配置的,与Service关联起来 ...

  2. [转] Java接口_interface_implements

    相对抽象类来讲,接口就是比抽象类还要抽象的抽象类,丝毫不带半点实现的内容.接口可以更加规范的对子类进行约束.接口全面地专业地实现了:规范和具体实现的分离.接口就是规范,定义的是一组规则,提现了现实世界 ...

  3. python全栈开发day35-线程、协程

    一.线程 1.线程 1).什么是线程 线程是cpu调度的最小单位 线程是进程的必要组成单位 一个进程里至少含有一个线程 2).主线程 程序开始运行的视乎,就产生了一个主线程来运行这个程序 3).子线程 ...

  4. BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...

  5. 8.Django-form组件

    1.form组件的校验功能 文件formsdemo models from django.db import models # Create your models here. class UserI ...

  6. MQ确认机制之事务机制----confirm串行

    一:介绍 1.说明原理 A:生产者将信道设置成confirm模式,一旦信道进到confirm模式,所有该信道上发布的消息都会被指派一个唯一的ID(从1开始). 一旦消息被投递到所有匹配的队列后,bro ...

  7. 015.Linux系统删根数据恢复

    重要声明: 本文档所有思路来自于誉天教育,由本人(木二)在实验环境验证通过: 您应当通过誉天邹老师或本人提供的其他授权通道下载.获取本文档,且仅能用于自身的合法合规的业务活动: 本文档的内容仅供学习交 ...

  8. angular中使用代理

    使用代理 1.在跟目录创建proxy.config.json文件 { "/api": { "target": "http://localhost:30 ...

  9. Quartz配置

    1. Quartz主要配置 属性名称 是否必选 类型 默认值 说明 org.quartz.scheduler.instanceName 否 String QuartzScheduler Schedul ...

  10. [ 原创 ] Centos7.6安装Mysql5.7

    https://blog.csdn.net/shj_php/article/details/86712408 CentOS7下安装MySQL5.7安装与配置(YUM) http://blog.csdn ...