python-leetcode-搜索二维矩阵

74. 搜索二维矩阵 - 力扣(LeetCode)


python 复制代码
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        if not matrix or not matrix[0]:
            return False
        
        m, n = len(matrix), len(matrix[0])
        left, right = 0, m * n - 1
        
        while left <= right:
            mid = left + (right - left) // 2
            row = mid // n
            col = mid % n
            
            if matrix[row][col] == target:
                return True
            elif matrix[row][col] < target:
                left = mid + 1
            else:
                right = mid - 1
        
        return False
相关推荐
天赐学c语言9 分钟前
12.10 - 合并两个有序链表 && 对字节对齐的理解
数据结构·c++·leetcode·链表
曹牧11 分钟前
Java:Jackson库序列化对象
java·开发语言·python
simon_skywalker11 分钟前
线性代数及其应用习题答案(中文版)第二章 矩阵代数 2.3 可逆矩阵的特征(2)
线性代数·矩阵
永远是夏天14 分钟前
Python矩阵索引与切片:单元素/行列/子矩阵提取全解析
python
simon_skywalker17 分钟前
线性代数及其应用习题答案(中文版)第二章 矩阵代数 2.4 分块矩阵(1)
线性代数·矩阵
JasonZhu42619 分钟前
pycharm 12月最新2025.3 安装、授权、使用说明
ide·python·pycharm
MediaTea20 分钟前
Python:依赖倒置原则(DIP)
开发语言·python·依赖倒置原则
冤大头编程之路23 分钟前
Matplotlib/Seaborn特征分布图、损失曲线等可视化绘制全攻略
python
路边草随风26 分钟前
python获取飞书文档内容
python·aigc·飞书
Q_Q51100828538 分钟前
python+django/flask+vue基于spark的西南天气数据的分析与应用系统
spring boot·python·spark·django·flask·node.js