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.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...
随机推荐
- Java Base64加密解密
使用Apache commons codec 类Base64 maven依赖 <dependency> <groupId>commons-codec</groupId&g ...
- 在使用Vs2013打开Vs2008的解决方案时出现了以下错误:此版本的应用程序不支持其项目类型(.csproj)
在使用Vs2013打开Vs2008的解决方案时出现了以下错误: 无法打开 因为此版本的应用程序不支持其项目类型(.csproj). 在网络上找到解决方案: 命令行或者Vs自带的命令提示符输入:deve ...
- CHtmlEditCtrl (2): Add a Source Text Editor to Your HTML Editor
In a previous article, I described how to create an HTML editor using the MFC CHtmlEditCtrl class in ...
- jchat-windows-master 编译输出日志
第一个项目成功生成的输出日志 >------ 已启动全部重新生成: 项目: QxOrm, 配置: Debug x64 ------ >Moc'ing IxModel.h... >Mo ...
- 【Python】torrentParser1.03
#------------------------------------------------------------------------------------ # torrentParse ...
- struct的初始化
1.struct的初始化可以使用类似数组的方式,如下:struct Student{ int _Age; string _Name;};Student stu = {26,"Andy&quo ...
- IT行业简报 2014-2-8
1.微信在“我的银行卡”页面接入嘀嘀打车,三天内微信打车突破10万单,日均订单为70万,其中微信支付订单超过48万单2.三大运营商手机支付用户仅366.3万,与腾讯单月发展手机支付用户500万户相比, ...
- vsphere 5.1 性能最佳实践。
1.关于CPU负载.extop显示的结果 如果CPU load average>=1,说明主机过载了. 如果PCPU used%在80%左右说明良好,90%以上就临近过载了. VM赋予过多的vC ...
- app_offline.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DT ...
- scala VS python2 (linux or shell)
PS:只考虑最新版的scala和python2.x,由于python3.x和python2.x区别比较大,而且主流的一些开源项目都用的python2,目前python2一点点在兼容python3 1. ...