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
相关推荐
IRevers10 分钟前
【自动驾驶】经典LSS算法解析——深度估计
人工智能·python·深度学习·算法·机器学习·自动驾驶
前端拿破轮11 分钟前
翻转字符串里的单词,难点不是翻转,而是正则表达式?💩💩💩
算法·leetcode·面试
倔强青铜三15 分钟前
苦练Python第8天:while 循环之妙用
人工智能·python·面试
凤年徐15 分钟前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
倔强青铜三40 分钟前
苦练Python第7天:布尔七日斩
人工智能·python·面试
倔强青铜三1 小时前
苦练Python第6天:数字魔法全解
人工智能·python·面试
蜗牛的旷野1 小时前
华为OD机试_2025_查找单入口空闲区域(Python,100分)(附详细解题思路)
python·算法·华为od
李昊_1 小时前
【LeetCode 3440. 重新安排会议得到最多空余时间 II】解析
算法·leetcode
倔强青铜三1 小时前
苦练Python第5天:字符串从入门到格式化
人工智能·python·面试
呆呆的小鳄鱼1 小时前
leetcode:322. 零钱兑换[完全背包]
算法·leetcode·职场和发展