当前位置:网站首页>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

边栏推荐
- LeetCode 24. Swap nodes in linked list pairwise
- StarRocks on AWS Review | Data Everywhere Series Event Shenzhen Station ended successfully
- CV复习:空洞卷积
- Polygon zkEVM工具——PIL和CIRCOM
- LeetCode 86. Delimited Linked List
- 娄底石油化工实验设计、建设规划概述
- How to do foreign media publicity to grasp the key points
- 基于PLECS的离网(孤岛)并联逆变器的Droop Control下垂控制仿真
- 漏洞管理计划的未来趋势
- 22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
猜你喜欢
随机推荐
这三个 Go 水平自测题,你手写不出来还是先老实上班吧,过来看看
CV复习:空洞卷积
47Haproxy集群
tommy's spell
讯飞创意组别 全国选拔赛成绩公布说明
How to cultivate the design thinking of ui designers?
odps sql 不支持 unsupported feature CREATE TEMPORARY
自定义过滤器和拦截器实现ThreadLocal线程封闭
StarRocks on AWS 回顾 | Data Everywhere 系列活动深圳站圆满结束
毕业总结
多线程下自旋锁设计基本思想
Threshold-based filtering buffer management scheme in a shared buffer packet switch论文核心部分
国外媒体宣发怎样做才可以把握重点
部署项目半途而废后续
StarRocks on AWS Review | Data Everywhere Series Event Shenzhen Station ended successfully
Servlet---解决post请求中中文乱码问题
The author of open source also has a life problem
Excel function formulas - HLOOKUP function
Diary 16
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...







