当前位置:网站首页>LeetCode中等题之搜索二维矩阵
LeetCode中等题之搜索二维矩阵
2022-08-10 12:05:00 【·星辰大海】
题目
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
每行中的整数从左到右按升序排列。
每行的第一个整数大于前一行的最后一个整数。
示例 1:
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:true
示例 2:
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
输出:false
提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 100
-10^4 <= matrix[i][j], target <= 10 ^4
来源:力扣(LeetCode)
解题思路
题目难度不大,直观的思路就是直接将矩阵flatten为1维数组,然后利用二分查找即可。
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
temp=[]
for i in matrix:
temp.extend(i)
return temp[bisect.bisect(temp,target)-1]==target
边栏推荐
- H264 GOP 扫盲
- Polygon zkEVM工具——PIL和CIRCOM
- The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp
- 漏洞管理计划的未来趋势
- 娄底石油化工实验设计、建设规划概述
- Color map and depth map to point cloud
- Excel函数公式大全—HLOOKUP函数
- 【list合并】多个list合并为一个list
- LeetCode 82. Remove Duplicate Elements in Sorted List II
- 一文详解 implementation api embed
猜你喜欢
随机推荐
LeetCode 82. Remove Duplicate Elements in Sorted List II
48MySQL数据库基础
搜索--09
CURRENT_TIMESTAMP(6) 函数是否存在问题?
leetcode/两个链表的第一个重合节点
Common examples of regular expressions
郭晶晶家的象棋私教,好家伙是个机器人
Chapter9 : De Novo Molecular Design with Chemical Language Models
时间序列的数据分析(五):简单预测法
Custom filters and interceptors implement ThreadLocal thread closure
百度用户产品流批一体的实时数仓实践
「网络架构」网络代理第一部分: 代理概述
加密游戏:游戏的未来
Accumulated and thin hair!Safety Dog has once again obtained the certification of scientific and technological achievements transformation!
[Collection] HashSet and ArrayList lookup Contains() time complexity
dedecms支持Word内容一键导入
7、Instant-ngp
16. Getting Started with Pytorch Lightning
The author of open source also has a life problem
Crypto Gaming: The Future of Gaming