240. 搜索二维矩阵 II


思路

遍历每一行,将当前行的数存放至哈希表中(in的时间复杂度O(1)),查询target是否存在当前行中,是直接返回

遍历结束都找不到target,则说明target不存在

python 复制代码
class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        for i in range(len(matrix)):
            s = set(matrix[i])
            if target in s:
                return True

        return False
相关推荐
吃着火锅x唱着歌14 小时前
LeetCode 3185.构成整天的下标对数目II
算法·leetcode·职场和发展
资深web全栈开发14 小时前
LeetCode 1590:使数组和能被 p 整除(前缀和 + 哈希表优化)
算法·leetcode·前缀和·算法优化·哈希表·go 语言·取模运算
CoderYanger14 小时前
递归、搜索与回溯-综合练习:27.黄金矿工
java·算法·leetcode·深度优先·1024程序员节
vx_vxbs6615 小时前
【SSM高校普法系统】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
android·java·python·mysql·小程序·php·idea
sin_hielo15 小时前
leetcode 1590
数据结构·算法·leetcode
吃着火锅x唱着歌15 小时前
LeetCode 2748.美丽下标对的数目
数据结构·算法·leetcode
做怪小疯子15 小时前
LeetCode 热题 100——二叉树——二叉树的中序遍历
算法·leetcode·职场和发展
田里的水稻15 小时前
Python_编程中代码注释相关格式 PEP8 — Python 官方代码风格指南
开发语言·python
丹宇码农15 小时前
consul集群搭建
python·consul