leetcode-240-搜索二维矩阵②
题目描述:

最佳方法:O(m+n) O(1)
class Solution:
def searchMatrix(self, matrix, target):
if not matrix :
return False row = len(matrix)
col = len(matrix[0])
i = 0
j = col-1
while i<row and j>=0:
if matrix[i][j] == target:
return True
elif matrix[i][j] > target:
j -= 1
else:
i += 1
return False
方法二:O(nlogn)*
class Solution:
def searchMatrix(self, matrix, target):
# an empty matrix obviously does not contain `target`
if not matrix:
return False def search_rec(left, up, right, down):
# this submatrix has no height or no width.
if left > right or up > down:
return False
# `target` is already larger than the largest element or smaller
# than the smallest element in this submatrix.
elif target < matrix[up][left] or target > matrix[down][right]:
return False mid = left + (right-left)//2 # Locate `row` such that matrix[row-1][mid] < target < matrix[row][mid]
row = up
while row <= down and matrix[row][mid] <= target:
if matrix[row][mid] == target:
return True
row += 1 return search_rec(left, row, mid-1, down) or search_rec(mid+1, up, right, row-1) return search_rec(0, 0, len(matrix[0])-1, len(matrix)-1)
leetcode-240-搜索二维矩阵②的更多相关文章
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- Java实现 LeetCode 240 搜索二维矩阵 II(二)
240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...
- Leetcode 240.搜索二维矩阵II
搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...
- leetcode 240搜索二维矩阵
/** 正常的二维搜索估计要超时,本题沿着对角线搜索,然后找到第一个大于目标数字的坐标(x,y)然后搜索(>x,<y)(<x,>y)子区域: 矩阵size() 为m,n:当i& ...
- LeetCode 240——搜索二维矩阵 II
1. 题目 2. 解答 2.1. 方法一 从矩阵的左下角开始比较 目标值等于当前元素,返回 true: 目标值大于当前元素,j 增 1,向右查找,排除掉此列上边的数据(都比当前元素更小): 目标值小于 ...
- LeetCode 240 - 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列.每列的元素从上到下升序排列.示例: 现有矩阵 matrix 如 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II)
题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 m ...
- Java实现 LeetCode 240 搜索二维矩阵 II
public static boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0) return false ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
随机推荐
- springBoot优雅返回图片/网页到浏览器
一.普通spring mvc返回图片或网页到浏览器 @Controller @RequestMapping(value = "/image") public class Image ...
- Mariadb 10.2.8版本GTID主从环境搭建以及切换
1.首先搭建主从 主环境:192.168.1.117 从环境:192.168.1.123 a.首先以二进制包的形式安装好MariaDB (忽略不计) b.配置环境的变量 通配 [mysqld] bin ...
- WordTEX
https://www.andrew.cmu.edu/user/twildenh/wordtex/
- 实战ZeroMQ的PUSH/PULL推拉模式
原文地址: http://ju.outofmemory.cn/entry/235976
- cmd操作SQLService数据库
1.win+R 输入cmd2.输入sqlcmd -s 服务器名称3. 1> 输入 use 数据库名称4. 2> go5. 1> select *from 表名6. 2> go
- leetcode-回溯③
题77 回溯: class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res = [] def backtr ...
- PHP PDO 大对象 (LOBs)
应用程序在某一时刻,可能需要在数据库中存储"大"数据. "大"通常意味着"大约 4kb 或以上",尽管某些数据库在数据达到"大&q ...
- NX二次开发-UFUN获取图层类别的信息UF_LAYER_ask_category_info
1 NX11+VS2013 2 3 #include <uf.h> 4 #include <uf_ui.h> 5 #include <uf_layer.h> 6 7 ...
- NX二次开发-常用lib库文件
在项目属性->配置属性->链接器->输入->附加依赖项: libufun.lib UFUNC API 函数库 libugopenint.lib UFUNC 对话框 API 函数 ...
- NX11.0和VS2013 创建NXOpen 开发模版失败解决方案【转载】
转载自PLM之家论坛 NX11.0和VS2013 创建NXOpen 开发模版失败解决方案 首先我觉得这个可能是西门子疏忽,基本上每个大版本没有补丁前都有类似问题,下面来说说怎么解决吧.注意这里版本,N ...