【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 ...
随机推荐
- Linux搭建yum仓库
1.安装nginx 2.为nginx搭建共享目录 3.安装createrepo,创建存储库 4.客户端测试 1.安装nginx yum list |grep nginx #查看是否有可用的nginx包 ...
- 学习java的第十四天
一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ...
- add more
# -*- coding: utf-8 -*- print('123', 123) print(type('123'), type(123)) # string, integer /ˈintidʒə/ ...
- day09 文件属性
day09 文件属性 昨日回顾 yum底层原理: 第一步:执行yum install nginx安装命令 第二步:yum去/etc/yum.repos.d这个目录中 第三步:根据/etc/yum/re ...
- HDFS【Java API操作】
通过java的api对hdfs的资源进行操作 代码:上传.下载.删除.移动/修改.文件详情.判断目录or文件.IO流操作上传/下载 package com.atguigu.hdfsdemo; impo ...
- fastjson过滤多余字段
/** * Description:过滤实体中的字段 * @param src 需要过滤的对象,如 list,entity * @param clazz 实体的class ...
- js处理title超长问题
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- weak和拷贝
weak/拷贝 1. weak 只要没有strong指针指向对象,该对象就会被销毁 2. 拷贝 NSString和block用copy copy语法的作用 产生一个副本 修改了副本(源对象)并不会影响 ...
- Spring Boot中使用Servlet与Filter
在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...
- 基于war的Spring Boot工程
一.简介 前面创建的Spring Boot工程最终被打为了Jar包,是以可执行文件的形式出现的,其使用了Spring Boot内嵌的Tomcat作为Web服务器来运行web应用的.新版Dubbo的监控 ...