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
相关推荐
xixihaha132417 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
超越自我肖17 小时前
python--while循环的基础案例
python
2501_9216494917 小时前
免费港股实时行情 API:功能、性能与接入指南
开发语言·后端·python·金融·restful
zh路西法17 小时前
【宇树机器人强化学习】(四):Go2基础训练以及参数调节与解析
python·深度学习·ubuntu·机器学习·机器人
码路飞17 小时前
315 曝光 AI 投毒产业链,我写了个 Python 脚本检测 AI 回答有没有「中毒」
python·aigc
q_354888515317 小时前
计算机毕业设计源码:锦江酒店大数据分析与个性化推荐系统 Django框架 Vue 可视化 Hadoop 爬虫 协同过滤推荐算法 民宿 客栈(建议收藏)✅
python·机器学习·信息可视化·数据分析·django·课程设计·旅游
一叶落43817 小时前
LeetCode 11:盛最多水的容器(C语言实现)
c语言·数据结构·算法·leetcode
_日拱一卒17 小时前
LeetCode(力扣):二叉树的后序遍历
算法·leetcode·职场和发展
sg_knight17 小时前
设计模式实战:代理模式(Proxy)
python·设计模式·代理模式·proxy
xixihaha132417 小时前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python