【数组】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 ...
随机推荐
- 删除重复的feature vba VS 删除重复的feature python
VBA: Sub deleteDuplicatedFeature() Dim app As IApplication Set app = Application Dim pMxDocument As ...
- org.apache.cxf 官方骨架
mvn archetype:generate -DarchetypeCatalog=remote 用 cxf 筛选结果 官方三大项目骨架 org.apache.cxf:cxf-http-basic ( ...
- HDU6026 Deleting Edges 2017-05-07 19:30 38人阅读 评论(0) 收藏
Deleting Edges Time ...
- uva 579 ClockHands 几何初接触 求时针与分针的夹角
貌似是第一次接触几何题... 求时针与分针的夹角,这不是小学生的奥数题么.我小时候也想过这问题的. 每过一小时时针走1/12*360=30度,每过一分钟时针走1/60*30=0.5度,分针走1/60* ...
- 后台返回的json数据传到前端页面并在页面的表格中填充
web前端页面: <table id="myTable" cellpadding="1" cellspacing="0" border ...
- Using Integrated SOA Gateway in Oracle EBS(websevice)
http://blog.csdn.net/pan_tian/article/details/10159935 Oracle EBS如何与第三方系统相集成?比如这样的需求,X系统知道物料编码,需要从EB ...
- Delphi for iOS开发指南(1):在Mac上配置你的开发环境
http://cache.baiducontent.com/c?m=9d78d513d99516f11ab7cf690d678c3b584380122ba7a0020fd18438e4732b4050 ...
- WinRT 中检查 WiFi 是否可用
public static bool IsWifiConnected() { bool isWifiConnected = false; ConnectionProfile currentConnec ...
- 纸壳CMS可视化建站系统搭建多语言网站
纸壳CMS是可视化建站系统,现已经从架构上支持多语言.但是多语言功能默认是没有开启的.您可以从设置中开启多语言,或者随时关闭它,您可以随时进行切换. 开启多语言 如果您没有在系统设置中看到多语言设置菜 ...
- C# 对象引擎,以路径形式访问对象属性(data.Product[1].Name)
对象引擎,以路径形式访问对象属性,例data.Product[1].Name. 在做excel模板引擎的时候,为了能方便的调用对象属性,找了一些模板引擎,不是太大就是不太适用于excel, 因为exc ...