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
相关推荐
长安er18 分钟前
LeetCode215/347/295 堆相关理论与题目
java·数据结构·算法·leetcode·
元亓亓亓26 分钟前
LeetCode热题100--62. 不同路径--中等
算法·leetcode·职场和发展
在屏幕前出油33 分钟前
二、Python面向对象编程基础——理解self
开发语言·python
小白菜又菜41 分钟前
Leetcode 1925. Count Square Sum Triples
算法·leetcode
阿方索1 小时前
python文件与数据格式化
开发语言·python
信创天地3 小时前
信创国产化数据库的厂商有哪些?分别用在哪个领域?
数据库·python·网络安全·系统架构·系统安全·运维开发
不哦罗密经3 小时前
python相关
服务器·前端·python
happybasic3 小时前
python字典中字段重复性的分析~~
开发语言·python
山海青风3 小时前
人工智能基础与应用 - 数据处理、建模与预测流程 6 模型训练
人工智能·python·机器学习
l木本I3 小时前
Reinforcement Learning for VLA(强化学习+VLA)
c++·人工智能·python·机器学习·机器人