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. Java设置Excel有效性

    XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("Excel"); String[] ...

  2. <CentOS7>如何设置hostname

    在CentOS/RHEL 7中,有个叫hostnamectl的命令行工具,它允许你查看或修改与主机名相关的配置: ceph@client-node ~]$ hostnamectlstatus Stat ...

  3. DirectFB 之 字体显示

    通过本文,可以简单地了解directfb字体内部运行机制. 简介 SetFont函数,是每次写字体前必须调用的一个函数,否则directfb程序将会报错.这个函数是将某种字体与某个surface相关联 ...

  4. scp 命令快速使用讲解

    在 Linux 下使用 scp 命令 scp 是安全拷贝协议(Secure Copy Protocol)的缩写,和众多 Linux/Unix 使用者所熟知的拷贝(cp)命令一样.scp 的使用方式类似 ...

  5. phtread_mutex 组合

    phtread_mutex通过mutexattr设定其类型,并保存在成员__kind中.pthread_mutex的锁操作函数根据__kind进行方法的分派(dispatch).__kind由5个字段 ...

  6. 数据转换d2d.js

    d2d.js what? d2d是data2data的简写,用来转换为符合需求的data. why? 我们在开发中定义好了的接口字段,后端工程可能因某些原因修改了字段, 或者我们用的插件或组件用到的数 ...

  7. SpringMVC+Spring 事务无法回滚的问题

    问题描述: Controller里面执行Service的方法,Service方法抛出异常,但是没有按照事务配置的方式回滚: Service的事务配置没有问题: 出现此问题的原因: 在springmvc ...

  8. canvas-弧形可拖动进度条

    一.效果如下: See the Pen XRmNRK by pangyongsheng (@pangyongsheng) on CodePen. 链接dome 二. 本文是实现可拖动滑块实现的基本,及 ...

  9. CountDownLacth详解

    一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CounDownLatch.由于调用了countDown() 方法,所以在当前计数到达零之 ...

  10. angular4 JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)

    最近在写基于angular4的项目的时候,在build --prod的时候,突然措手不及的蹦出个报错,大致错误如下: 70% building modules 1345/1345 modules 0 ...