【数组】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 ...
随机推荐
- 20155336 2016-2017-2《JAVA程序设计》第六周学习总结
20155336 2016-2017-2<JAVA程序设计>第六周学习总结 教材学习内容总结 第十章 串流(Stream): 数据有来源及目的地,衔接两者的是串流对象.如果要将数据从来源取 ...
- SPSS-相关性和回归分析(一元线性方程)案例解析
任何事物和人都不是以个体存在的,它们都被复杂的关系链所围绕着,具有一定的相关性,也会具备一定的因果关系,(比如:父母和子女,不仅具备相关性,而且还具备因果关系,因为有了父亲和母亲,才有了儿子或女儿), ...
- DEVEXPRESS 破解方法
Devexpress 是.net的一个非常好用的插件.能够轻松的帮你实现一个非常炫的UI,无论是C#的Winform还是ASP.NET的网站. 鄙人这两天在用DEVEXPRESS的过程中发现在网上并未 ...
- (回文串 Manacher )Girls' research -- hdu -- 3294
http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit:1000MS Memory Limit:32 ...
- hdu 2063 匈牙利算法
http://acm.hdu.edu.cn/showproblem.php?pid=2063 男女配对最大数 匈牙利算法模板 #include <cstdio> #include < ...
- nginx backend 健康检查
ngx_http_proxy_module 模块和ngx_http_upstream_module模块(自带) 严格来说,nginx自带是没有针对负载均衡后端节点的健康检查的,但是可以通过默认自带的n ...
- python——排序
从学校毕业出来后只知道冒泡排序,发现自己对排序的了解还是很浅显. 于是在网上搜索各种排序方法,以下是本人根据索搜出来的资料再结合自己理解作出的一些简单的阐述. 如果有不正确的地方欢迎大家指正.(共同学 ...
- 4.C#WebAPI多版本管理介绍及实现方案详解
1.什么是 API 的多版本? 说白了就是多版本共存的问题.为方便大家理解我就举个例子吧,大家想必都用过Jquery吧,它的1.*版本做到了对低版本IE的支持:2.*版本还保留着ajax,但是不再支持 ...
- GitHub设置使用SSH Key,用TortoiseGit进行Clone仓库
GitHub设置使用SSH Key的好处就是可以使用SSH连接,并且提交代码的时候可以不用输入密码,免密提交. 生成SSH Key 这里我们使用PuTTYgen来生成公钥(Public Key),私钥 ...
- Win(Phone)10开发第(2)弹,导出APPX包并签名部署
当我们新建一个win10 uap项目,如果想导出测试包,需要点击项目名称,选择商店-导出应用包,这个时候会生成一个文件夹,包含appx和ps1等文件. powershell运行Add-AppDevPa ...