题目描述:

最佳方法: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-搜索二维矩阵②的更多相关文章

  1. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  2. Java实现 LeetCode 240 搜索二维矩阵 II(二)

    240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...

  3. Leetcode 240.搜索二维矩阵II

    搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...

  4. leetcode 240搜索二维矩阵

    /** 正常的二维搜索估计要超时,本题沿着对角线搜索,然后找到第一个大于目标数字的坐标(x,y)然后搜索(>x,<y)(<x,>y)子区域: 矩阵size() 为m,n:当i& ...

  5. LeetCode 240——搜索二维矩阵 II

    1. 题目 2. 解答 2.1. 方法一 从矩阵的左下角开始比较 目标值等于当前元素,返回 true: 目标值大于当前元素,j 增 1,向右查找,排除掉此列上边的数据(都比当前元素更小): 目标值小于 ...

  6. LeetCode 240 - 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列.每列的元素从上到下升序排列.示例: 现有矩阵 matrix 如 ...

  7. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II)

    题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 m ...

  8. Java实现 LeetCode 240 搜索二维矩阵 II

    public static boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0) return false ...

  9. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  10. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

随机推荐

  1. vue组件的inheritAttrs属性

    vue官网对于inheritAttrs的属性解释:如果你不希望组件的根元素继承特性,你可以在组件的选项中设置 inheritAttrs: false. 可能不是很好理解,我们可以举个例子来验证一下. ...

  2. 第十篇 scrapy item loader机制

    在我们执行scrapy爬取字段中,会有大量的和下面的代码,当要爬取的网站多了,要维护起来很麻烦,为解决这类问题,我们可以根据scrapy提供的loader机制 def parse_detail(sel ...

  3. EtherCat开源主站SOEM在windows下工程配置

    截至2019年1月,虽然github上有1.3.3的更新,但是用的人估计还不多,而且1.3.2被官方认为bug严重弃用.所以还是学习1.3.1. 1.拷贝osal.oshw.soem.lib(已生成l ...

  4. springBoot优雅返回图片/网页到浏览器

    一.普通spring mvc返回图片或网页到浏览器 @Controller @RequestMapping(value = "/image") public class Image ...

  5. 【学术篇】SPOJ FTOUR2 点分治

    淀粉质入门第一道 (现在个人认为spoj比bzoj要好_(:з」∠)_ 关于点分治的话推荐去看一看漆子超的论文>>>这里这里<<< 之前一直试图入点分治坑, 但是因 ...

  6. easyui datagrid 绑定从后台得到的复杂的特殊数据结构

    由于项目需要,从后台得到的数据统一为了类似{state:xxx,data:xxx,message:xxx}类型 但是easyui datagrid却只认{total:xxx,rows:xxx}...所 ...

  7. c++内存相关函数

    memset void *memset(void *s, int ch, size_t n); 函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ...

  8. jq-demo-购物车

    首页 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title& ...

  9. Batch - FOR %%a %%b

    总结 %%a refers to the name of the variable your for loop will write to. Quoted from for /?: FOR %vari ...

  10. 【安装】Mac rabbitMQ

    安装 brew install rabbitmq 目录  cd /usr/local/Cellar/rabbitmq/3.7.4/sbin 插件 sudo ./rabbitmq-plugins ena ...