[LeetCode] 74. Search a 2D Matrix_Medium tag: Binary Search
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.
Example 1:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true
Example 2:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false 1) 第一种思路为 因为每行是sorted, 并且后面的row要比前面的要大, 所以我们可以将它看做是一维的sorted array 去处理, 然后去Binary Search, 只是需要注意的是num = matrix[mid//lrc[1]:列数][mid%lrc[0]:行数]. 2) 第二种思路为, 找到可能在某一行(last index <= target),然后在该行中做常规的binary search
Code 1)
class Solution:
def searchMatrix(self, matrix, target):
if not matrix or len(matrix[0]) == 0: return False
lrc = [len(matrix), len(matrix[0])]
l, r = 0, lrc[0]*lrc[1] -1
while l + 1 < r:
mid = l + (r - l)//2
num = matrix[mid//lrc[1]][mid%lrc[1]]
if num > target:
r = mid
elif num < target:
l = mid
else:
return True
if target in [matrix[l//lrc[1]][l%lrc[1]], matrix[r//lrc[1]][r%lrc[1]]]:
return True
return False
2)
class Solution:
def searchMatrix(self, matrix, target):
if not matrix or len(matrix[0]) == 0 or matrix[0][0] > target or matrix[-1][-1] < target:
return False
colNums = list(zip(*matrix))[0] # first column
# find the last index <= target
l, r = 0, len(colNums) -1
while l + 1 < r:
mid = l + (r - l)//2
if colNums[mid] > target:
r = mid
elif colNums[mid] < target:
l = mid
else:
return True
rowNums = matrix[r] if colNums[r] <= target else matrix[l]
l, r = 0, len(rowNums) - 1
while l + 1 < r:
mid = l + (r -l)//2
if rowNums[mid] > target:
r = mid
elif rowNums[mid] < target:
l = mid
else:
return True
if target in [rowNums[l], rowNums[r]]: return True
return False
[LeetCode] 74. Search a 2D Matrix_Medium tag: Binary Search的更多相关文章
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
Description Given a sorted array of n integers, find the starting and ending position of a given tar ...
- [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 278. First Bad Version_Easy tag: Binary Search
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [LeetCode] 704. Binary Search_Easy tag: Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
随机推荐
- CodeForces - 754D
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring s ...
- 使用介质设备安装 AIX 以通过 HMC 安装分区
使用介质设备安装 AIX 以通过 HMC 安装分区 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.h ...
- String和datetime在SQL中和在C#中相互转换方法总结
Custom Date and Time Format Strings <= https://docs.microsoft.com/en-us/dotnet/standard/base-ty ...
- 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作
题面:P3178 [HAOI2015]树上操作 好像其他人都嫌这道题太容易了懒得讲,好吧那我讲. 题解:第一个操作和第二个操作本质上是一样的,所以可以合并.唯一值得讲的点就是:第二个操作要求把某个节点 ...
- mysql 语句 字段 和结构主键外键的增删改
primary key 主键 notnull 不为空 unique 唯一 foreign key(外键) references t1(id) auto_increment ...
- ubuntu创建用户的两种方式
ubuntu创建用户有两种方式: useradd和adduser 这两者,就像零件与产品的关系.useradd是DIY,需要自己调配,adduser是品牌机,拿来就能用. 对于创建一般用户来讲,use ...
- MapReduce 找出共同好友
这个前提需要注意:好友之间的关系是单向的,我的好友队列里有你,你的里面不一定有我.所以思考方式需要改变. 共同好友: 某两个人的好友队列里都有的人. 第一个mapper 和 reducer 简单说:找 ...
- manifold 微分流形上可以定义可微函数、切向量、切向量场、各种张量场等对象并建立其上的分析学,并可以赋予更复杂的几何结构以研究它们的性质。
小结: 1.流形(英语:Manifolds)一般可以通过把许多平直的片折弯并粘连而成,是局部具有欧几里得空间性质的空间,是欧几里得空间中的曲线.曲面等概念的推广 2.描述一个流形往往需要不止一个“地图 ...
- Linux之sed、awk
Linux 之AWK 命令 简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在对数据分析并生成报告时,显得尤为强大. 简单来说awk就是把文件逐行的读入,以空格默认分隔 ...
- innodb表锁情况
MySQL InnoDB默认行级锁.行级锁都是基于索引的 行级锁变为表级锁情况如下: 1.如果一条SQL语句用不到索引是不会使用行级锁的,会使用表级锁把整张表锁住. 2.表字段进行变更. 3.进行整表 ...