Leetcode 74 and 240. Search a 2D matrix I and II

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.

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.

第一题的条件比第二题还强,也就是本行的第一个元素比上一行的最后一个元素要大。所以如果第一题把2D矩阵拉平成一个1D list,这个list是一个sorted list。可以再用二分法查找。

 1 class Solution(object):
 2     def searchMatrix(self, matrix, target):
 3         """
 4         :type matrix: List[List[int]]
 5         :type target: int
 6         :rtype: bool
 7         """
 8         if not matrix:
 9             return False
10
11         nums = []
12         for elem in matrix:
13             nums += elem
14         n = len(nums)
15
16         low, high = 0, n-1
17         while low <= high:
18             mid = (low+high)//2
19             if target < nums[mid]:
20                 high = mid -1
21             elif target > nums[mid]:
22                 low = mid + 1
23             else:
24                 return True
25         return False

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.

For 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.

第二题不能像第一题那样把2D matrix拉成一个1D list。可以使用类似kth smallest element in a sorted matrix中的类似查找方法。

 1 def searchMatrix(self, matrix, target):
 2         """
 3         :type matrix: List[List[int]]
 4         :type target: int
 5         :rtype: bool
 6         """
 7         m, n = len(matrix), len(matrix[0])
 8         i, j = m - 1, 0
 9
10         while i >= 0 and j < n:
11             if matrix[i][j] > target:
12                 i -= 1
13             elif matrix[i][j] < target:
14                 j += 1
15             else:
16                 return True
17         return False
时间: 2024-10-06 08:52:12

Leetcode 74 and 240. Search a 2D matrix I and II的相关文章

刷题240. Search a 2D Matrix II

一.题目说明 题目240. Search a 2D Matrix II,从一个m*n的二维矩阵查找一个整数,每一行从左到右递增,每一列从上到下递增. 二.我的解答 先计算矩阵中点matrix[row_mid][col_mid],然后将矩阵分成4个区间: class Solution{ public: bool dfs(vector<vector<int> >& matrix,int target,int start_x, int end_x, int start_y, in

[LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

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.

【LeetCode】240. Search a 2D Matrix II

Search a 2D Matrix II 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 ascendin

[leedcode 240] Search a 2D Matrix II

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.

【leetcode】74. Search a 2D Matrix &amp; 240. Search a 2D Matrix II

题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是,如果某一行的最小值都比target要大,那么这一行之后的值都比target要大.如果target介于某一行的最小值和最大值之间,那么target有可能在这一行.至于如何判断target是否存在,因为数组有序,用二分查找即可. 代码如下: class Solution(object): def se

[LeetCode#240] Search a 2D Matrix II

Problem: 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

[二分搜索] leetcode 240 Search a 2D Matrix II

problem:https://leetcode.com/problems/search-a-2d-matrix-ii 经典双指针二分查找题目. class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(); if(!m) return false; int n = matrix[0].size(); if(!n) ret

Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

1.问题描述 写一个高效的算法,从一个m×n的整数矩阵中查找出给定的值,矩阵具有如下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 根据矩阵的特征很容易想到二分法,但是这是一个二维的矩阵,如何将问题转化为一维是关键.实际上我们可以根据矩阵的第一列确定值可能所在的行的范围(limu,limd),其中limu=0,使得matrix[0][0]≤matrix[i][0]≤matrix[limd][0],i∈[0,limd].而确定limd的值可以使用二分法.

(medium)LeetCode 240.Search a 2D Matrix II

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.