[Leetcode] Binary search, Divide and conquer--240. Search a 2D Matrix II
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)
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的更多相关文章
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【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 ...
- 【刷题-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 ...
- 240.Search in a 2D Matrix II
/* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Swift try try! try?使用和区别
Swift try try! try?使用和区别 一.异常处理try catch的使用 1. swift异常处理 历史由来 Swift1.0版本 Cocoa Touch 的 NSError ,Swif ...
- javascript的null 和undifined
null表示一个对象,或者变量的值为空,undifined 表示声明了一个对象(变量),没有给他初始化或者压根儿就没有声明这个对象(变量). 1.null 是JavaScript关键字 undifin ...
- Java中Properties类
1 简介: JDK提供的java.util.Properties类继承自Hashtable类并且实现了Map接口,用map来存储key-value数据,所以存入的数据是无序的.其中键和值都是字符串类型 ...
- Bootstrap之折叠(Collapse)插件
学习资料:Bootstrap折叠(Collapse)插件 大家可能常见的都是类似: 这种的效果,小颖今天要给大家分享一个不一样的效果嘻嘻.铛铛铛铛........................... ...
- iOS 制作自动打包脚本 Xcode8.3.2
本文包含以下内容: 前言 1.shell脚本的编写 2.xcodebuild命令 3.完整的可用示例 参考资料 前言 做iOS开发,打包APP是比较频繁的事情,每次都手动去配置一堆东西确实是比较乏味. ...
- jenkins 用户名密码忘记
进入c盘--用户 在本地用户的目录下找到.jenkins目录,里面有一个config.xml; 打开后,删除其中的 " <useSecurity>true</useSecu ...
- java泛型探索——小特性
泛型特性(小篇幅) 1. 补充介绍一些常见的泛型特性: 类型参数T可以是recursive(类似递归性),它的边界可以是类型参数是自身的接口或类. 如我实现寻找最大值的方法,可以这么写: public ...
- 属于自己的MES(一)概念
什么叫MES(生产制造执行系统)? 从几个方面来简单说下: 1.定位 没有MES前的工厂生产模式,公司MRP系统与生产现场之间透过人为方式沟通,使生产现场如同黑箱作业,无法掌握实时正确信息. MES的 ...
- hadoop配置文件的作用
core-site.xml <property> //指定hdfs的主端口 namenode要放在哪台机器上 <name>fs.defaultFS</name> & ...
- hdu2571 命运 简单DP
简单dp 状态方程很好想,主要是初始化.... 代码: #include<iostream> #include<cstdlib> #include<cstdio> ...