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 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
用户8356290780517 小时前
使用 C# 在 Excel 中创建数据透视表
后端·python
码路飞10 小时前
FastMCP 实战:一个 .py 文件,给 Claude Code 装上 3 个超实用工具
python·ai编程·mcp
dev派12 小时前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪13 小时前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户03321266636713 小时前
将 PDF 文档转换为图片【Python 教程】
python
xlp666hub14 小时前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
悟空爬虫15 小时前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派15 小时前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风17 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python