LeetCode 240. Search a 2D Matrix II

简介: 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:每行的元素从左到右升序排列。每列的元素从上到下升序排列。

v2-285ad9de8ee78f84f966b7b7c3d59454_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 in ascending from left to right.

Integers in each column are sorted in ascending from top to bottom.


Example:


Consider the following matrix:

[

[1, 4, 7, 11, 15],

[2, 5, 8, 12, 19],

[3, 6, 9, 16, 22],

[10, 13, 14, 17, 24],

[18, 21, 23, 26, 30]

]

Given target = 5, return true.

Given target = 20, return false.


描述



编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:

每行的元素从左到右升序排列。

每列的元素从上到下升序排列。


示例:


现有矩阵 matrix 如下:

[

[1, 4, 7, 11, 15],

[2, 5, 8, 12, 19],

[3, 6, 9, 16, 22],

[10, 13, 14, 17, 24],

[18, 21, 23, 26, 30]

]

给定 target = 5,返回 true。

给定 target = 20,返回 false。


思路



  • 我们从矩阵的右上角开始检查.
  • 如果当前位置的元素大于target,由于当前列的元素都大于当前位置的元素,说明target不可能在当前列中,我们向左走一列.
  • 如果当前位置的元素小于target,由于当前行的所有元素都小于当前位置的元素(当前位置后面的元素不在比较范围之内,因为已经通过刚才的列判断了target不可能在这些列中),说明target不可能在当前行中,我们向下走一行.
  • 这样不断向左下走的过程中,如果遇到了target值,返回Ture,如果走到了左下角都没有遇到,返回Fasle.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-02-02 13:01:11
# @Last Modified by:   何睿
# @Last Modified time: 2019-02-02 13:45:27
class Solution:
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        if not matrix or not matrix[0]: return False
        # 从矩阵的右上角开始寻找(左下角也可以)
        # 从右上角开始,如果向左走则所有的元素递减;如果向下走所有的元素递增
        row, col = 0, len(matrix[0]) - 1
        while row < len(matrix) and col >= 0:
            # 如果相等,返回True
            if matrix[row][col] == target: return True
            # 如果当前位置元素更大,由于当列的元素都大于当前元素
            # 即target不在当前列中,col自减一次
            if matrix[row][col] > target: col -= 1
            # 如果当前位置的元素更小,由于当前元素在行的末尾
            # (当前元素后面的元素已经被列判定给否定了)
            # 当前元素前面的元素前面的元素都小于当前元素
            # 即target不在当前行中,row自增一次
            if matrix[row][col] < target: row += 1
        return False

源代码文件在这里.


目录
相关文章
Leetcode 74. Search a 2D Matrix
这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。 假设矩阵是m*n,扫一遍的时间复杂度就是O(m*n),题目中给出的这么特殊的矩阵,时间复杂度可以降到O(m+n),具体代码如下,写的比较挫。
138 1
Leetcode 240. Search a 2D Matrix II
具体思路就是每一行倒着扫,扫到第一个比target小的数就跳到下行,如果等于当然是直接返回true了,如果下一行还比target小就继续跳下一行,直到最后一行。 为啥这么做是可行的? 可能我比较笨,想了半天才想到。 因为每一列都是增序的,举个例子,假设matrix[0][5] > target,那么[0][5]位置右下(包含右和下)所有元素不可能比target小。
101 0
|
Python
LeetCode 378. Kth S Element in a Sorted Matrix
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。
167 0
LeetCode 378. Kth S Element in a Sorted Matrix
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
139 0
LeetCode 329. Longest Increasing Path in a Matrix
|
算法
LeetCode 74. Search a 2D Matrix
编写一个有效的算法,搜索m×n矩阵中的值。 此矩阵具有以下属性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.
165 0
LeetCode 74. Search a 2D Matrix
|
存储 索引
LeetCode 73. Set Matrix Zeroes
给定一个m * n 的矩阵,如果当前元是0,则把此元素所在的行,列全部置为0. 额外要求:是否可以做到空间复杂度O(1)?
210 0
LeetCode 73. Set Matrix Zeroes
|
索引
LeetCode 54. Spiral Matrix
给定m×n个元素的矩阵(m行,n列),以螺旋顺序[顺时针]返回矩阵的所有元素
118 0
LeetCode 54. Spiral Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 59. Spiral Matrix II
给定正整数n,以螺旋顺序生成填充有从1到n2的元素的方阵。
178 0

热门文章

最新文章