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
相关推荐
徐同保2 小时前
python异步函数语法解析,async with ... as ...语法解析
数据库·python·oracle
m***06682 小时前
SpringBoot项目中读取resource目录下的文件(六种方法)
spring boot·python·pycharm
eWidget3 小时前
数据可视化进阶:Seaborn 柱状图、散点图与相关性分析
数据库·python·信息可视化·kingbase·数据库平替用金仓·金仓数据库
清水白石0084 小时前
Python 柯里化完全指南:从函数式思想到工程实践
linux·服务器·python
滴滴答滴答答4 小时前
LeetCode Hot100 之 16 合并两个有序链表
算法·leetcode·链表
圣保罗的大教堂4 小时前
leetcode 3713. 最长的平衡子串 I 中等
leetcode
myzzb4 小时前
纯python 最快png转换RGB截图方案 ——deepseek
开发语言·python·学习·开源·开发
宸迪5 小时前
【python】使用uv管理项目包依赖
linux·python·uv
qq_448011165 小时前
python中的内置globals()详解
开发语言·python