leetcode74:二维矩阵搜索问题
使用递归的方式解决,对于matrix,在左上角x,y,右下角xx,yy组成的区域内搜索target。
mx=x和xx的中点,my=y和yy的中点
判断matrix[mx][my],如果它大于target,则左上角四分之一区域无需再搜;如果它小于target,则右下角四分之一区域无需再搜。但是右上角和左下角有可能需要搜索。复杂度为log(行×列)
class Solution:
def solve(self, matrix, x, y, xx, yy, target):
if target > matrix[xx - 1][yy - 1] or target < matrix[x][y]: return False
if x + 1 >= xx and y + 1 >= yy:
if matrix[x][y] == target:
return True
else:
return False
mx = (x + xx) >> 1
my = (y + yy) >> 1
if matrix[mx][my] > target:
ans = self.solve(matrix, x, y, mx, my, target)
else:
ans = self.solve(matrix, mx, my, xx, yy, target)
if ans: return True
ans = self.solve(matrix, mx, y, xx, my, target) or self.solve(matrix, x, my, mx, yy, target)
return ans
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix: return False
r = len(matrix)
if not r: return False
c = len(matrix[0])
if not c: return False
return self.solve(matrix, 0, 0, r, c, target)
if __name__ == '__main__':
print(Solution().searchMatrix([[1, 3]], 1))
leetcode74:二维矩阵搜索问题的更多相关文章
- LeetCode74.搜索二维矩阵
74.搜索二维矩阵 描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 示 ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- lintcode:搜索二维矩阵II
题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...
- lintcode :搜索二维矩阵
题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...
- LeetCode(74):搜索二维矩阵
Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 ...
- 240. 搜索二维矩阵 II
二维数组搜索 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...
随机推荐
- oracle的(+)
(+)就是连接譬如SELECT a.*, b.* from a(+) = b就是一个右连接,等同于select a.*, b.* from a right join bSELECT a.*, b.* ...
- windows及linux环境下永久修改pip镜像源的方法
一.在windows环境下修改pip镜像源的方法(以python3.5为例) (1):在windows文件管理器中,输入 %APPDATA% (2):会定位到一个新的目录下,在该目录下新建pip文件夹 ...
- linux下性能分析命令[总结]
1.前言 在linux下开发程序,为了追求高性能,经常需要测试程序的性能,包括cpu.内存.io.网络等等使用情况.liunx下提供了众多命令方便查看各种资源的使用情况.经常用的有ps.top.fre ...
- dubbo Framework pic
dubbo Framework pic
- MonoDB的数据准备
首先是数据的录入,为了分析我们服务器集群的性能,需要准备大量的用户数据,幸运的是mtools提供了mgenerate方法供我们使用.他可以根据一个数据模版向 MongoDB 中插入任意条 json ...
- [React] Write a stateful Component with the React useState Hook and TypeScript
Here we refactor a React TypeScript class component to a function component with a useState hook and ...
- 【nodejs】FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
当使用大批量(>100)的SQL进行MySql数据库插值任务时,会发生以下错误: 总计将有371579条数据将被插入数据库 开始插入DB <--- Last few GCs ---> ...
- 国外某牛人的JsonModelBinder 实现 MVC 3.0
public class JsonModelBinder : DefaultModelBinder { public override object BindModel(ControllerConte ...
- C++ Standard Library
C++ Standard Library *注:内容主要是对參考1的学习记录.知识点与图片大都来源于该书, 部分知识点与图片来源于參考2. 详细參考信息,见最下方參考. * C++98中新支持的语言特 ...
- 阅读《Android 从入门到精通》(9)——多项选择
多项选择(CheckBox) CheckBox 类是 Button 的子类,层次关系例如以下: android.widget.Button android.widget.CompoundButton ...