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
相关推荐
算法与编程之美5 分钟前
理解pytorch中的L2正则项
人工智能·pytorch·python·深度学习·机器学习
R-G-B10 分钟前
【P19 机器学习-分类算法及应用实践】手写数字识别(KNN)
python·机器学习·分类·手写数字识别·knn算法
Eric.Lee202119 分钟前
ubuntu系统在bashrc文件中对conda进行启用设置
linux·运维·python·ubuntu·conda
观音山保我别报错21 分钟前
变量作用域
开发语言·python
卿雪22 分钟前
Redis的数据类型 + 底层实现:String、Hash、List、Set、ZSet
数据结构·数据库·redis·python·mysql·缓存·golang
清水白石00824 分钟前
什么是猴子补丁(Monkey Patch)?生产环境能用吗?——实战导读
python·安全·系统安全
xiaoqi97663369028 分钟前
免费文字转语音助手 python+edge_tts+FFMPEG
python·edge·ffmpeg
APIshop31 分钟前
用“爬虫”思路做淘宝 API 接口测试:从申请 Key 到 Python 自动化脚本
爬虫·python·自动化
子午35 分钟前
【农作物谷物识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
红蒲公英37 分钟前
( 教学 )Agent 构建 Prompt(提示词)3. StructuredOutputParser (结构化输出)
人工智能·python·prompt