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
相关推荐
zm-v-159304339861 分钟前
Python 气象数据处理从入门到精通:机器学习订正 + 深度学习预测完整教程
python·深度学习·机器学习
老鼠只爱大米2 分钟前
LeetCode经典算法面试题 #215:数组中的第K个最大元素(快速选择、堆排序、计数排序等多种实现方案详解)
算法·leetcode·堆排序·快速选择·topk·数组中的第k个最大元素
逆境不可逃11 分钟前
LeetCode 热题 100 之 35. 搜索插入位置 74. 搜索二维矩阵 34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode
1941s12 分钟前
Google Agent Development Kit (ADK) 指南 第四章:Agent 开发与编排
人工智能·python·langchain·agent·adk
布谷歌19 分钟前
Fastjson枚举反序列化:当字符串不是枚举常量名时,会发生什么?
开发语言·python
虚幻如影20 分钟前
python识别验证码
开发语言·python
今儿敲了吗20 分钟前
python基础学习笔记第七章——文件操作
笔记·python·学习
Austin_YB21 分钟前
VScode中配置Python环境
ide·vscode·python
qq_4523962322 分钟前
【Python × AI】LangChain 深度剖析:从组件解耦到 LCEL 的逻辑美学
人工智能·python·ai·langchain
ChineHe23 分钟前
基础篇003_Python基础语法
开发语言·人工智能·python