搜索二维矩阵【二分】

Problem: 74. 搜索二维矩阵

文章目录

思路 & 解题方法

可以二分一次,也可以二分两次。

复杂度

时间复杂度:

添加时间复杂度, 示例: O ( l o g n + l o g m ) O(logn + logm) O(logn+logm)

空间复杂度:

添加空间复杂度, 示例: O ( 1 ) O(1) O(1)

二分两次

python 复制代码
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        m, n = len(matrix), len(matrix[0])
        top, but = 0, m - 1
        while top < but:
            mid = (top + but) // 2
            if matrix[mid][-1] < target:
                top = mid + 1
            elif matrix[mid][-1] >= target:
                but = mid
        if but >= 0 and but < m:
            left, right = 0, n - 1
            l = matrix[but]
            while left <= right:
                mid = (left + right) // 2
                if l[mid] > target:
                    right = mid - 1
                elif l[mid] < target:
                    left = mid + 1
                else:
                    return True
        return False

二分一次

python 复制代码
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        m, n = len(matrix), len(matrix[0])
        for l in matrix:
            if l[-1] < target:
                continue
            else:
                left, right = 0, n - 1
                while left <= right:
                    mid = (left + right) // 2
                    if l[mid] < target:
                        left = mid + 1
                    elif l[mid] > target:
                        right = mid - 1
                    else:
                        return True
                break
        return False
相关推荐
云云3213 天前
亚矩阵云手机:破解 Yandex 广告平台多账号风控难题的利器
网络·科技·线性代数·智能手机·矩阵
我想静静wwwwww3 天前
74.搜索二维矩阵
数据结构·算法·矩阵
快去睡觉~3 天前
力扣73:矩阵置零
算法·leetcode·矩阵
我.佛.糍.粑4 天前
Shusen Wang推荐系统学习 --召回 矩阵补充 双塔模型
人工智能·学习·机器学习·矩阵·推荐算法
爱吃涮毛肚的肥肥(暂时吃不了版)4 天前
剑指offer——模拟:顺时针打印矩阵
算法·leetcode·矩阵
ChoSeitaku5 天前
NO.4数据结构数组和矩阵|一维数组|二维数组|对称矩阵|三角矩阵|三对角矩阵|稀疏矩阵
数据结构·人工智能·矩阵
ComputerInBook5 天前
矩阵之方阵与行列式的关系
线性代数·矩阵·行列式·线性变换·方阵的行列式
passxgx6 天前
9.2 埃尔米特矩阵和酉矩阵
线性代数·矩阵
盛寒6 天前
等价矩阵和等价向量组
线性代数·算法·矩阵
WoShop商城源码6 天前
短视频矩阵系统的崛起:批量发布功能与多平台矩阵的未来
人工智能·线性代数·其他·矩阵