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
相关推荐
拼命鼠鼠1 天前
【算法】矩阵链乘法的动态规划算法
算法·矩阵·动态规划
LYFlied1 天前
【每日算法】LeetCode 17. 电话号码的字母组合
前端·算法·leetcode·面试·职场和发展
爱笑的眼睛111 天前
超越MSE与交叉熵:深度解析损失函数的动态本质与高阶设计
java·人工智能·python·ai
Rose sait1 天前
【环境配置】Linux配置虚拟环境pytorch
linux·人工智能·python
过期动态1 天前
JDBC高级篇:优化、封装与事务全流程指南
android·java·开发语言·数据库·python·mysql
一世琉璃白_Y1 天前
pg配置国内数据源安装
linux·python·postgresql·centos
liwulin05061 天前
【PYTHON】COCO数据集中的物品ID
开发语言·python
小鸡吃米…1 天前
Python - XML 处理
xml·开发语言·python·开源
我赵帅的飞起1 天前
python国密SM4加解密
python·sm4加解密·国密sm4加解密
一起养小猫1 天前
LeetCode100天Day1-字符串匹配与Z字形变换
java·leetcode