【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛
 id: fuxuemingzhu
 个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/search-a-2d-matrix/description/
题目描述
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.
 
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.
题目大意
怪我理解能力太差?谁告诉我和74. Search a 2D Matrix的区别?
给出了有规则的二维矩阵,规则是从左向右依次递增,从上向下依次递增。进行查找。
解题方法
和74. Search a 2D Matrix完全一样的代码就A了。。我都蒙的。
下面是解释。
这个题在剑指offer的38-40页有详细解释。方法是从右上角向左下角进行遍历,根据比较的大小决定向下还是向左查找。
剑指offer的解释是我们从矩阵的左下角或者右上角开始遍历,这样知道了比较的结果是大还是小,就知道了对应的前进方向。
Python代码:
class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        if not matrix or not matrix[0]:
            return False
        rows = len(matrix)
        cols = len(matrix[0])
        row, col = 0, cols - 1
        while True:
            if row < rows and col >= 0:
                if matrix[row][col] == target:
                    return True
                elif matrix[row][col] < target:
                    row += 1
                else:
                    col -= 1
            else:
                return False
C++代码如下:
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if (matrix.size() == 0 || matrix[0].size() == 0) return false;
        const int M = matrix.size(), N = matrix[0].size();
        int i = M - 1, j = 0;
        while (i >= 0 && j < N) {
            if (matrix[i][j] == target) {
                return true;
            } else if (matrix[i][j] < target) {
                ++j;
            } else {
                --i;
            }
        }
        return false;
    }
};
方法二:
暴力解法遍历,这也能过。。我实在看不懂leetcode了。。
class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        return any(target in row for row in matrix)
日期
2018 年 3 月 6 日
 2019 年 1 月 7 日 —— 新的一周开始啦啦啊
【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)的更多相关文章
- [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 240. Search a 2D Matrix II
		
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
 - (medium)LeetCode  240.Search a 2D Matrix II
		
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
 - Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
		
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
 - 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
		
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
 - 【刷题-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 & 240. Search a 2D Matrix II
		
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
 - 240 Search a 2D Matrix II 搜索二维矩阵 II
		
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列.例如,考虑下面的矩阵:[ [1, 4, 7 ...
 
随机推荐
- 一款真正可以拿的出手的本土嵌入式RTOS-SylixOS
			
由 winniewei 提交于 周四, 12/20/2018 作者:张国斌 在参加工信部人才交流中心和南京浦口区开发区管委会联合举办的第三届集成电路产业紧缺人才创新发展高级研修班暨产业促进交流会期间, ...
 - 19. 删除链表的倒数第 N 个结点
			
目录 19.删除链表的倒数第N个节点 题目 题解-暴力 题解-哈希表 题解-双指针 19.删除链表的倒数第N个节点 题目 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点. 输入:he ...
 - linux RPM/YUM包管理
			
linux RPM/YUM包管理 目录 linux RPM/YUM包管理 RPM RPM包管理 查询rpm包 卸载rpm包 安装rpm包 YUM 查看yum服务器是否有需要安装的软件 下载安装指定的y ...
 - nuxt使用图片懒加载vue-lazyload
			
对于nuxt使用第三方插件的方式大体都是都是一致的,就是在plugins文件夹中新增插件对应的js文件进行配置与操作,然后在nuxt.config.js文件的plugins配置项中引入新建的js文件就 ...
 - SpringBoot Logback 日志配置
			
目录 前言 日志格式 日志输出 日志轮替 日志级别 日志分组 小结 前言 之前使用 SpringBoot 的时候,总是习惯于将日志框架切换为 Log4j2,可能是觉得比较靠谱,也可能年龄大了比较排斥新 ...
 - 移动开发之h5学习大纲
			
移动开发学习形式:授课.自学 1.html5 css3 htm5shiv.js response.js 2.流式布局 自适应布局 盒模型 弹性盒模型 响应式布局3.iscroll swiper boo ...
 - Hibernate持久化标志符生成策略
			
generator子元素定义持久化标识符的生成策略,为持久化类对应的数据库表的主键找到了赋值方法,HIbernate默认将使用assigned的持久化标识符生成策略.关系型数据库的主键定义方式:(1) ...
 - 【MySQL】排名函数
			
https://www.cnblogs.com/shizhijie/p/9366247.html 排名函数 主要有rank和dense_rank两种 区别: rank在排名的时候,排名的键一样的时候是 ...
 - TSN 时间敏感网络:缘起 (TSN历史与现状)
			
前言 随着工业物联网(IIoT)的兴起和工业4.0的提出,越来越多的设计师.工程师和最终用户关注时间敏感网络(Time-Sensitive Networking,下简称为TSN).TSN为以太网提供确 ...
 - IOS 真机调试和发布相关证书
			
一.成员介绍1. Certification(证书)证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种:1) Developer Certification(开发证书)安装在电脑上 ...