题目:

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的更多相关文章

  1. [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 ...

  2. [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 ...

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

  4. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

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

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

  6. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

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

  8. [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 ...

  9. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

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

随机推荐

  1. Java解决高并发方案(帮助你我他)

           一个小型的网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单.随着互联网业务的不断丰富,网站 ...

  2. angular2+ 初理解

    一.Angular Module     1.angular 模块是一个类,它需要NgModule这个装饰器函数接受一个原数据对象作为参数来描述这个模块类属性.     其中最重要的属性有:      ...

  3. ajax如何向后台传递数组,在后台该如何接收的问题(项目积累)

    一.后台如何接收从前台接收的数组: 使用request.getParameterValues(String xxx); <input type="text" name=&qu ...

  4. PYQT5实现文件目录浏览

    def setBrowerPath(self): download_path = QtWidgets.QFileDialog.getExistingDirectory(self, "浏览&q ...

  5. [翻译]ASP.NET Web API 2 中的全局错误处理

    目录 已存在的选项 解决方案预览 设计原则 什么时候去用 方案详情 示例 附录: 基类详情 原文链接 Global Error Handling in ASP.NET Web API 2 由于翻译水平 ...

  6. CentOS7布署.Net Core

    本文记录的所有东西,都是吸取别人的经验,自己实践得来,记录下来,也是为了某一天,用上的时候,能够更加的得心应手,平日的学习,都是未雨绸缪之举,但愿,这些笔记,也能帮上正在摸索的你. 第一步,下载虚拟机 ...

  7. 初识WebAPI

    (一)Web API简介: 目前使用Web服务的三种主流的方式是:远程过程调用(RPC),面向服务架构(SOA)以及表征性状态转移(REST),其中REST模式的Web服务与复杂的SOA和RPC对比来 ...

  8. C#一个简单的关于线程的实例

    很多初学者听到线程会觉得晦涩难懂,很多资料一堆专有名词也是让人心烦意乱,本着学习加分享的态度,这里做一个简单的实例分享帮助初学者们初识多线程.  首先大概讲述一下多线程和多进程的区别,任务管理器里各种 ...

  9. BitAdminCore框架更新日志20180518

    20180518更新内容 1.重构调整QQ登录代码,使用JObject,减少代码,增加access_token自动续期(未测试). 2.重构调整微信登录代码,使用JObject,减少代码,增加acce ...

  10. WPF 自定义下拉列表

    XAML代码: <Popup x:Name="popupStrategy" StaysOpen="False" PopupAnimation=" ...