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
相关推荐
iAkuya9 分钟前
(leetcode)力扣100 14合并区间(差分/排序)
算法·leetcode·职场和发展
weixin_4215850140 分钟前
PYTHON 迭代器1 - PEP-255
开发语言·python
月明长歌1 小时前
【码道初阶】【LeetCode 958】判定完全二叉树:警惕 BFS 中的“管中窥豹”陷阱
算法·leetcode·宽度优先
hxxjxw1 小时前
Pytorch分布式训练/多卡训练(六) —— Expert Parallelism (MoE的特殊策略)
人工智能·pytorch·python
dagouaofei2 小时前
PPT AI生成实测报告:哪些工具值得长期使用?
人工智能·python·powerpoint
BoBoZz192 小时前
ExtractPolyLinesFromPolyData切割一个三维模型(球体),并可视化切割后产生的多条等高线
python·vtk·图形渲染·图形处理
quikai19812 小时前
python练习第六组
java·前端·python
Trouville012 小时前
Python中encode和decode的用法详解
开发语言·python
月明长歌3 小时前
【码道初阶】【LeetCode 102】二叉树层序遍历:如何利用队列实现“一层一层切蛋糕”?
java·数据结构·算法·leetcode·职场和发展·队列