LeetCode 74. Search a 2D Matrix

简介: 编写一个有效的算法,搜索m×n矩阵中的值。 此矩阵具有以下属性:每行中的整数从左到右排序.每行的第一个整数大于前一行的最后一个整数.

v2-ec5ae56363a9caa092bbb7d4cd4b6d04_1440w.jpg


Description



Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:


Integers in each row are sorted from left to right.

The first integer of each row is greater than the last integer of the previous row.


Example 1:


Input:

matrix = [

[1, 3, 5, 7],

[10, 11, 16, 20],

[23, 30, 34, 50]

]

target = 3

Output: true


Example 2:


Input:

matrix = [

[1, 3, 5, 7],

[10, 11, 16, 20],

[23, 30, 34, 50]

]

target = 13

Output: false


描述



编写一个有效的算法,搜索m×n矩阵中的值。 此矩阵具有以下属性:

每行中的整数从左到右排序.

每行的第一个整数大于前一行的最后一个整数.


思路



  • 给定一个排好序的二维数组,查找一个值,如果该值在数组中返回Ture,不在返回False.
  • 此题目考察二分法.


class Solution:
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        # 如果矩阵为空,返回False
        if not matrix or not matrix[0]:
            return False
        # 二分法遍历,首先确定target在哪一行
        left, right, middle = 0, len(matrix)-1, 0
        while left <= right:
            middle = left+((right-left) >> 1)
            if matrix[middle][0] < target:
                left = middle+1
            elif matrix[middle][0] > target:
                right = middle-1
            else:
                return True
        row = right
        left, right = 0, len(matrix[0])-1
        # 二分法遍历,确定当前值在哪一个位置
        while left <= right:
            middle = left+((right-left) >> 1)
            if matrix[row][middle] < target:
                left = middle+1
            elif matrix[row][middle] > target:
                right = middle - 1
            else:
                return True
        return False
if __name__ == "__main__":
    so = Solution()
    res = so.searchMatrix(
        [[]], 1001)
    print(res)


源代码文件在这里.

目录
相关文章
|
7月前
Leetcode 74. Search a 2D Matrix
这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。 假设矩阵是m*n,扫一遍的时间复杂度就是O(m*n),题目中给出的这么特殊的矩阵,时间复杂度可以降到O(m+n),具体代码如下,写的比较挫。
65 1
|
7月前
Leetcode 240. Search a 2D Matrix II
具体思路就是每一行倒着扫,扫到第一个比target小的数就跳到下行,如果等于当然是直接返回true了,如果下一行还比target小就继续跳下一行,直到最后一行。 为啥这么做是可行的? 可能我比较笨,想了半天才想到。 因为每一列都是增序的,举个例子,假设matrix[0][5] > target,那么[0][5]位置右下(包含右和下)所有元素不可能比target小。
33 0
|
Python
LeetCode 378. Kth S Element in a Sorted Matrix
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。
83 0
LeetCode 378. Kth S Element in a Sorted Matrix
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
53 0
LeetCode 329. Longest Increasing Path in a Matrix
LeetCode 304. Range Sum Query 2D - Immutable
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。
84 0
LeetCode 304. Range Sum Query 2D - Immutable
|
算法
LeetCode 240. Search a 2D Matrix II
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性: 每行的元素从左到右升序排列。 每列的元素从上到下升序排列。
63 0
LeetCode 240. Search a 2D Matrix II
LeetCode 212. Word Search II
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
61 0
LeetCode 212. Word Search II
|
索引
LeetCode 81. Search in Rotated Sorted Array II
假设按升序排序的数组在事先未知的某个枢轴处旋转。 (例如, [0,0,1,2,2,5,6] 可能变成 [2,5,6,0,0,1,2]). 给定一个要搜索的目标值T, 如果该目标值能在数组中找到则返回true,否则返回false。
69 0
LeetCode 81. Search in Rotated Sorted Array II
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix