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.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...
随机推荐
- 【Scala】Scala-None-null引发的血案
Scala-None-null引发的血案 Overview - Spark 2.2.0 Documentation Spark Streaming - Spark 2.2.0 Documentatio ...
- 【math】梯度下降法(梯度下降法,牛顿法,高斯牛顿法,Levenberg-Marquardt算法)
原文:http://blog.csdn.net/dsbatigol/article/details/12448627 何为梯度? 一般解释: f(x)在x0的梯度:就是f(x)变化最快的方向 举个例子 ...
- ASP入门(九)-Request对象小案例
我们将制作一个能够记住访问者姓名的页面,在这个小案例中,你将学会如何使用Request对象的Cookies.Form以及ServerVariables集合的值,还可以学习到如何使用Response对象 ...
- UltraISO制作U盘启动盘安装Win7/9/10系统攻略
U盘安装好处就是不用使用笨拙的光盘,光盘还容易出现问题,无法读取的问题.U盘体积小,携带方便,随时都可以制作系统启动盘. U盘建议选择8G及以上大小的. 下面一步一步讲解如果制作U盘安装盘: 1.首先 ...
- 基于spring与mockito单元测试Mock对象注入
转载:http://www.blogjava.net/qileilove/archive/2014/03/07/410713.html 1.关键词 单元测试.spring.mockito 2.概述 单 ...
- 【Python】使用geopy由地址找经纬度等信息
代码: from geopy.geocoders import Nominatim geolocator = Nominatim() location = geolocator.geocode(&qu ...
- ImportError DLL load failed: %1 不是有效的 Win32 应用程序
操作系统:win7 64位,安装mysqldb 后提示:ImportError DLL load failed: %1 不是有效的 Win32 应用程序,是由于安装的32位的 MySQL-Python ...
- PowerDesigner设计的数据库 ORA-0092
异常 数据库由Powerdesigner设计,格式为Oracle10g,由Powerdesigner生成的数据库并没报什么异常,使用navicat也能正常操作,而使用PLSQL Developer去出 ...
- Initialization failed for Block pool <registering> (Datanode Uuid unassigned) service to IP1:8020 Invalid volume failure config value: 1
2017-02-27 16:19:44,739 ERROR datanode.DataNode: Initialization failed for Block pool <registerin ...
- HDS Truecopy实现原理及项目的选择-诸多案例
copy from:http://www.eygle.com/archives/2009/05/hds_truecopy_dataguard.html 诸多案例:http://wenku.baidu. ...