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 from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

第一题的条件比第二题还强,也就是本行的第一个元素比上一行的最后一个元素要大。所以如果第一题把2D矩阵拉平成一个1D list,这个list是一个sorted list。可以再用二分法查找。

 class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix:
return False nums = []
for elem in matrix:
nums += elem
n = len(nums) low, high = 0, n-1
while low <= high:
mid = (low+high)//2
if target < nums[mid]:
high = mid -1
elif target > nums[mid]:
low = mid + 1
else:
return True
return False

如果不把2D matrix转换成1D array,可以直接操作row 和 col。注意L17-L18的对于从1D的位置到row col的转换方法。

 class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix:
return False m = len(matrix)
n = len(matrix[0])
l , r = 0, n*m -1 while l <= r:
mid = l + (r - l)/2
row = mid/ n
col = mid% n
if matrix[row][col] == target:
return True
elif matrix[row][col] < target:
l = mid + 1
else:
r = mid - 1
return False

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.

第二题不能像第一题那样把2D matrix拉成一个1D list。可以使用类似kth smallest element in a sorted matrix中的类似查找方法。

 def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
m, n = len(matrix), len(matrix[0])
i, j = m - 1, 0 while i >= 0 and j < n:
if matrix[i][j] > target:
i -= 1
elif matrix[i][j] < target:
j += 1
else:
return True
return False

Leetcode 74 and 240. Search a 2D matrix I and II的更多相关文章

  1. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  2. [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 ...

  3. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【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 ...

  5. 【刷题-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 ...

  6. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

  7. (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 ...

  8. 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 ...

  9. [LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. mysql 判断 字段是否为空

    SB.Append("select "); SB.Append("mpe.EVIDENCE_ID "); SB.Append("left join m ...

  2. Alwayson--SYS.dm_hadr_instance_node_map 返回0行

    下面是MSDN关于SYS.dm_hadr_instance_node_map 的解释,我就不在翻译了 For every instance of SQL Server that hosts an av ...

  3. 推薦使用 Microsoft Anti-Cross Site Scripting Library V3.0

    原文出至: http://blog.miniasp.com/post/2009/07/29/Recommand-Microsoft-Anti-Cross-Site-Scripting-Library- ...

  4. QT 网络编程一

    QT如果要进行网络编程首先需要在.pro中添加如下代码:QT += network 在头文件中包含相关头文件 #include <QHostInfo> #include <QNetw ...

  5. CentOS 7.0 Nvidia显卡安装步骤

    from: http://blog.sina.com.cn/s/blog_49c0985a0102v3fa.html CentOS 7.0 Nvidia显卡安装步骤: 1 在英伟达官网下载相应驱动 搜 ...

  6. lecture15-自动编码器、语义哈希、图像检索

    Hinton第15课,本节有课外读物<Semantic Hashing>和<Using Very Deep Autoencoders for Content-Based Image ...

  7. 虚拟机开机提示Operating System not found解决办法

    为了更好体验windows更多操作系统,有些用户会在VMware虚拟机中安装XP.win7或win8等等系统,有用户反映在虚拟机中安装XP开机后提示"Operating System not ...

  8. .Net分布式异常报警系统-简介

    系统简介 分布式异常报警系统就是收集系统运行过程中产生的未处理异常,检查系统运行的状态,并将异常信息统一发送到服务端,由服务端将信息通知到相关的责任人.  问题 我们在项目开发中可能遇到以下几个问题: ...

  9. express-partials与express4.x不兼容问题

    在express中设置view engine为html,express-partials会导致语法不正确,其实只要做一行代码的改动就可以 function renderer(ext){ if(ext[ ...

  10. [BZOJ3156]防御准备(斜率优化DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP