Leetcode 74 and 240. Search a 2D matrix I and 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 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的更多相关文章
- 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】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】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
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
- (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
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_Medium tag: Binary Search
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- NodeJS中的静态资源管理服务
欢迎大家指导与讨论 : ) 一.什么是静态资源 静态资源就是放在服务器中的特定的文件.比较常见的有.css,.png, .js的这一些后缀的文件.下图中的这个html页面便要获取到logo.png和a ...
- Post model至Web Api创建或是保存数据
前一篇<Post model至Web Api>http://www.cnblogs.com/insus/p/4343538.html中,使用Post来从Web Api获取数据.由于Post ...
- noi题库(noi.openjudge.cn) 1.9编程基础之顺序查找T01——T05
T01 查找特定元素的值 描述 在一个序列(下标从1开始)中查找一个给定的值,输出第一次出现的位置. 输入 第一行包含一个正整数n,表示序列中元素个数.1 <= n <= 10000.第二 ...
- JS使构造函数与new操作符无关
function User(name, passwordHash) { this.name = name; this.passwordHash = passwordHash; } 当使用User函数创 ...
- UDP的坏处
众所周知,UDP是一个面向无连接的协议.通信时不可靠的.这就会出现一些问题 (1)数据报丢失 因为是无连接,的所以可以用recvfrom和sendto来接收和发送消息,如果socket是阻塞的,那么当 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 为什么VC经常输出烫烫烫烫烫烫烫烫
为什么VC经常输出烫烫烫烫烫烫烫烫 2012-05-07 11:52 by Rollen Holt, 12747 阅读, 4 评论, 收藏, 编辑 在Debug 模式下, VC 会把未初始化的栈内存全 ...
- C# 控制台程序实现 Ctrl + V 粘贴功能
代码主要分为两部分,首先调用系统API注册剪切板相关的事件,然后监控用户的按键操作.完整代码如下: class ClipBoard { [DllImport("user32.dll" ...
- DLL丢失修复
DLL丢失修复,简答傻瓜式! DirectX修复工具(DirectX Repair)是一款系统级工具软件,简便易用.本程序为绿色版,无需安装,可直接运行. 本程序的主要功能是检测当前系统的Dir ...
- [AJAX系列]XMLHttpResponse对象
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...