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.

For 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.

Solution:

1. 1st idea use one binary search

iterate every rows , binary search from row i to find the insertion position p (bisect_right)

   m = len(matrix), n = len(matrix[0]) time complexity O(m*logn)
  if len(matrix) == 0 or len(matrix[0]) == 0:
return False
i = 0
while (i < len(matrix)):
pos = bisect_right(matrix[i], target) #use binary search
#print(" aaas: ",i, pos-1)
if matrix[i][pos-1] == target:
return True
i += 1
return False

2. 2nd utilize the ordered row and column,  start from bottom left value v, if v is bigger than target, then go the upper column, else go the right row. time complexity o(m+n)

 if len(matrix) == 0 or len(matrix[0]) == 0:
return False
m = len(matrix)
n = len(matrix[0])
i = m-1 #row
j = 0 #column
while ( i >= 0 and j <= n-1):
if matrix[i][j] == target:
return True
elif matrix[i][j] > target:
i -= 1
else:
j += 1
return False

3. we can also use divide and conquer:

reference:

 

[Leetcode] Binary search, Divide and conquer--240. Search a 2D Matrix II的更多相关文章

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

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

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

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

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

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

  4. 【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 ...

  5. 【刷题-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 ...

  6. 240.Search in a 2D Matrix II

    /* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...

  7. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

  8. LintCode 38. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...

  9. 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, ret ...

随机推荐

  1. storm kafkaSpout 踩坑问题记录! offset问题!

    整合kafka和storm例子网上很多,自行查找 问题描述: kafka是之前早就搭建好的,新建的storm集群要消费kafka的主题,由于kafka中已经记录了很多消息,storm消费时从最开始消费 ...

  2. bzoj4816 [Sdoi2017]数字表格

    Description Doris刚刚学习了fibonacci数列.用f[i]表示数列的第i项,那么 f[0]=0 f[1]=1 f[n]=f[n-1]+f[n-2],n>=2 Doris用老师 ...

  3. Eclipse 如何添加Window Builder插件?

    http://www.eclipse.org/windowbuilder/download.php 找到对应版本的window builder 如果不知道版本的话,可以在你的Eclipse-help- ...

  4. BM算法详解

    http://www-igm.univ-mlv.fr/~lecroq/string/node14.html http://www.cs.utexas.edu/users/moore/publicati ...

  5. elasticsearch系列(三)分表分库

    首先ES没有库和表的概念,只有index,type,document(详细术语可以看ES的系列一 http://www.cnblogs.com/ulysses-you/p/6736926.html), ...

  6. JDK版本会影响项目部署

    最近在公司里面部署javaweb项目的时候,项目启动的时候报错,我使用了各种方法来寻找答案,将近花了很长的时间.就在今天我终于找到了问题的根源,我开始用的是JDK1.8的版本,换了一个1.7版本的JD ...

  7. 在linux服务器上发布web应用的完整过程

    首先你要有一个完整的web应用的小Demo,一个简单的demo就可以了,但是要涉及到数据库,笔者这里简单的模拟一个登陆的过程. 在本地测试,访问项目: 键入账号密码,点击登陆: 就是这么个简单的动作, ...

  8. java中多种写文件方式的效率对比实验

    一.实验背景 最近在考虑一个问题:“如果快速地向文件中写入数据”,java提供了多种文件写入的方式,效率上各有异同,基本上可以分为如下三大类:字节流输出.字符流输出.内存文件映射输出.前两种又可以分为 ...

  9. Charles Proxy 4.1.2 破解版

    下载Charles Proxy 4.1.2版本,百度云盘下载 或 去官网下载 安装后先打开Charles一次(Windows版可以忽略此步骤) 在这个网站(http://charles.iiilab. ...

  10. webapi “ObjectContent`1”类型未能序列化内容类型“application/xml; charset=utf-8”的响应正文。

    今天在来一发  webapi的一个知识点 相信用过webapi的对这个错误 已经看在眼里 痛在心里了把 我百度也搜了一下  看了一下   然后发现他们的解决办法 并没有什么软用. 然后想起来当时上学的 ...