150强访谈简单

搜索二维矩阵

“搜索二维矩阵”问题的详细指南和 Python 实现。

问题陈述

简单

给定一个 m x n 整数矩阵 matrix ,具有以下两个属性:

- 每行按非降序排序。

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

给定一个整数 target,如果 target 位于 matrix 中,则返回 True ,否则返回 False

您必须以 O(log(m * n)) 时间复杂度编写解决方案。

编写一个函数 searchMatrix(matrix: List[List[int]], target: int) -> bool

约束条件
  • m == len(matrix)
  • n == len(matrix[i])
  • 1 <= m, n <= 100
  • -10^4 <= matrix[i][j], target <= 10^4

示例

Example 1
Input
matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], target = 3
Output
True
Explanation

3 is found in the first row at index 1.

Example 2
Input
matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], target = 13
Output
False
Explanation

13 is not present in the matrix.

Need a Hint?
考虑使用二分搜索特定的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。