【数组】Search a 2D Matrix
题目:
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 from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3
, return true
.
思路:
分两步,(1)先二分搜索的元素定位到行:当目标小于第一列某个元素时,向前面的行中去搜索;当目标大于第一列某个元素分两种情况 a、大于该元素所在行的最后一个元素时,往后面的行中去搜索,b、小于等于该元素所在行的最后一个元素,则可以定位到该元素所在的行。(2)在定位好的行中二分搜索
注意,在第一步查找所在行时,while(l<r)而不是whilel<=r;当target>nums[middle]时,还需要判断一下target和该行末的值,从而确定是否需要l=middle+1。
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
var m=matrix.length,n=matrix[0].length; var L=0,R=m-1,middle=0;
while(L<R){
middle=L+Math.floor((R-L)/2);
if(target<matrix[middle][0]){
R=middle-1;
}else if(target>matrix[middle][0]){
if(target>matrix[middle][n-1]){
L=middle+1;
}else{
L=middle;
break;
}
}else{
return true;
}
} var row=L;
var l=0,r=n-1,middle=0;
while(l<=r){
middle=l+Math.floor((r-l)/2);
if(matrix[row][middle]>target){
r=middle-1;
}else if(matrix[row][middle]<target){
l=middle+1;
}else{
return true;
}
}
return false; };
【数组】Search a 2D Matrix的更多相关文章
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 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 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- 28. Search a 2D Matrix 【easy】
28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
随机推荐
- [unchecked] 对作 为原始类型Hashtable的成员的put(K,V)的调用未经过检查。。。
问题: C:\Users\Administrator\Desktop\java\SoundApplet.java:212: 警告: [unchecked] 对作为原始类型Hashtable的成员的pu ...
- DOM数据解析
//DOM : Document Object Model ,一种XML解析的方式,先读取整篇的内容,然后再进行解析,解析速度比较快,如果内出现错误,解析就会失败,iOS不支持DOM解析,goole提 ...
- Android APK反编译步骤
反编译步骤 1.通过Android Killer 打开apk,自动开始分析 2.分析结束后,在分析好的工程上右键->打开方式->打开文件位置 在文件夹ProjectSrc中有文 ...
- (KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746
http://acm.hdu.edu.cn/showproblem.php?pid=3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others ...
- Codeforces805B. 3-palindrome 2017-05-05 08:33 156人阅读 评论(0) 收藏
B. 3-palindrome time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- hdu 1164 Eddy's research I
http://acm.hdu.edu.cn/showproblem.php?pid=1164 题意很简单,只是写代码的时候需要注意几个问题 一.筛选素数的时候记得用埃式筛选法,要是直接找可能会WA. ...
- poj2462
看八戒在做这个题,我也做了做.. 坑很多,还是要注意细节.不得不吐槽,难道又到了计算几何只能套模板否则就一串WA的情况了么! 要不是八戒做出来了,这题我估计我也就扔到这里了..哥不服啊~所以得做出来! ...
- 团队博客-第六周:Alpha阶段项目复审(科利尔拉弗队)
团队的排名-点评:以下排名点评谨代表个人观点,如有冒犯,评论联系删除 小组名字和链接 优点 缺点,bug报告(至少140字) 最终名次(无并列) 中午吃啥队 微信小程序应用,新型app会是一个便利的使 ...
- 别具光芒Div CSS 读书笔记(一)
继承 边框(border).边界(margin).填充(padding).背景(background) 是不能继承的. table 中td不会继承body的属性,因此需要单独指定. 权重 ...
- log Log4NET配置
Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过 日志查看系统的运行过程,从而发现系统的问题.日志的作用:将 ...