Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

Example:

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

这个题目思路, 1. brute force  T: O(m*n)    2. T: O(m+n)     3. T: O(lg(n!))  先找第一行, 第一列, 2n 再从(1,1) 开始扫第二行第二列, 2(n-1), .... 所以是lgn + lg (n-1) + lg(n-2)...lg(1) = lg(n!)

1) T: O(m*n)

两个for loop即可.

2) T: O(m + n)

        #O(n + m) , O(1)
if not matrix or len(matrix[0]) == 0: return False
lr, lc = len(matrix), len(matrix[0])
r, c = lr - 1, 0
while r >= 0 and c < lc:
if matrix[r][c] > target:
r -= 1
elif matrix[r][c] < target:
c += 1
else:
return True
return False

3)  T: O(lg(n!))

        # T: O(lg(n!))   S: O(1)
def helper(i, flag):
l = i
r = lrc[0]-1 if flag == 'row' else lrc[1] -1if flag == 'col':
while l + 1 < r:
mid = l + (r - l)//2
if matrix[i][mid] > target:
r = mid
elif matrix[i][mid] < target:
l = mid
else:
return True
if target in [matrix[i][l], matrix[i][r]]:
return True
return False
else:
while l + 1 <r:
mid = l + (r - l)//2
if matrix[mid][i] > target:
r = mid
elif matrix[mid][i] < target:
l = mid
else:
return True
if target in [matrix[l][i], matrix[r][i]]:
return True
return False if not matrix or len(matrix[0]) == 0: return False
lrc = [len(matrix), len(matrix[0])]
for i in range(min(lrc)):
row_check = helper(i, "row")
col_check = helper(i, "col")
if row_check or col_check: return True
return False

[LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search的更多相关文章

  1. [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  2. [LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  3. [LeetCode] 278. First Bad Version_Easy tag: Binary Search

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

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

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

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

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

  6. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  7. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

随机推荐

  1. C#打印标签

    一个复杂的标签包括一个复杂的表格样式和二维码.条形码等内容.所以如果直接绘制的方式将会非常的麻烦,所以采用使用的方案是使用模板的方式:1.使用Excel创建出想要的模板的样式.2.对模板中的动态内容进 ...

  2. 浅析vue的双向数据绑定

    vue 的双向数据绑定是基于es5的 object对象的defineProperty属性,重写data的set和get函数来实现的.1.defineProperty 数据展示 Object.defin ...

  3. .NET Core开发日志——Controller

    在理清路由的工作流程后,接下来需要考虑的,是MVC框架如何生成Controller以及它的生成时机. 根据以前ASP.NET MVC的经验,Controller应该是由一个ControllerFact ...

  4. select cast(round(12.5,2) as numeric(5,2))

    http://www.jb51.net/article/74284.htm 解释: round()函数,是四舍五入用,第一个参数是我们要被操作的数据,第二个参数是设置小数四舍五入的精度. )--32. ...

  5. JavaScript三种弹出框(alert,confirm和prompt)用法举例

    http://blog.csdn.net/lucky51222/article/details/45604681 我们在做网页交互的时候往往需要用户在操作之前弹出一个提示消息框来让用户做一些点击才能继 ...

  6. [No0000B0]ReSharper操作指南1/16-入门与简介

    安装指南 在安装之前,您可能需要检查系统要求. ReSharper是一个VisualStudio扩展.它支持VisualStudio2010,2012,2013,2015和2017.安装完成后,您将在 ...

  7. PCIe 复位:Clod reset、warm reset、Hot reset、Function level reset

    2015年09月06日 17:06:01 yijingjing17 阅读数:9029 标签: PCIEReSet复位Clod resetwarm reset 更多 个人分类: PCIe        ...

  8. 网关 整理 fastcgi wsgi

    https://www.cnblogs.com/hzhtracy/p/4365938.html 网关协议学习:CGI.FastCGI.WSGI.uWSGI   一直对这四者的概念和区别很模糊,现在就特 ...

  9. [Day5]方法

    1.方法 (1)概念:方法就是用来完成解决某件事情或实现某个功能的办法 会包含很多条语句用于完成某些有意义的功能 通过在程序代码中引用方法名称和所需的参数,实现在该程序中执行(或称调用)该方法 (2) ...

  10. Maven基本介绍及安装

    什么是Maven 是一个跨平台的项目管理工具. 跨平台是指它几乎可以在现有所有流行的操作系统中运行 maven不仅可以构建项目,还可以依赖管理和项目信息管理 Maven解决了什么问题 maven解决了 ...