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
相关推荐
我的xiaodoujiao15 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 25--数据驱动--参数化处理 Excel 文件 2
前端·python·学习·测试工具·ui·pytest
DO_Community19 分钟前
基于AI Agent模板:快速生成 SQL 测试数据
人工智能·python·sql·ai·llm·ai编程
Kuo-Teng2 小时前
LeetCode 118: Pascal‘s Triangle
java·算法·leetcode·职场和发展·动态规划
Q_Q5110082852 小时前
python+django/flask的宠物用品系统vue
spring boot·python·django·flask·node.js·php
hmbbcsm2 小时前
练习python题目小记(五)
开发语言·python
蓝桉~MLGT2 小时前
Python学习历程——文件
python·学习·策略模式
循环过三天2 小时前
7.5、Python-匿名函数lambda
笔记·python·学习
仟濹3 小时前
【Java 基础】3 面向对象 - this
java·开发语言·python
Dxy12393102163 小时前
Python一个类的特殊方法有哪些
开发语言·python