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.

分析:题意为在一个mxn矩阵中查找目标值。可以先通过二分法确定目标值target可能出现的行,然后再用一次二分法确定目标值target在行中的可能位置。

时间复杂度为O(logn+logm)

code如下:

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int left=0;
int right=matrix.size()-1;
if(left != right){
while(left <= right){
int mid=left + (right-left)/2;
if(matrix[mid][0]<target){
left=mid+1;
}
else if(matrix[mid][0]>target){
right=mid-1;
}
else {
return true;
}
}
}
if(right==-1){
return false;
}
else{
int row=right;
int left=0;
int right=matrix[row].size()-1;
while(left<=right){
int mid=left + (right-left)/2;
if(matrix[row][mid]<target){
left=mid+1;
}
else if(matrix[row][mid]>target){
right=mid-1;
}
else {
return true;
}
}
return false;
}
}
};

其他思路:

从左下角元素开始遍历,每次遍历中若与目标值target相等则返回true;若小于则列向右移动;若大于则行向下移动。时间复杂度O(logn+logm)
code如下:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int i=matrix.size()-1;
int j=0;
int m=matrix.size();
int n=matrix[0].size();
while(i>=0 && j<n){
if(matrix[i][j] > target){
i--;
}
else if(matrix[i][j] == target){
return true;
}
else{
j++;
}
}
return false;
}
};

  

  

leetcode:Search a 2D Matrix(数组,二分查找)的更多相关文章

  1. LeetCode Search a 2D Matrix(二分查找)

    题意: 有一个矩阵,每行都有序,每行接在上一行尾后仍然有序.在此矩阵中查找是否存在某个数target. 思路: 这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了.由于用vector ...

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

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

  3. Search a 2D Matrix——两度二分查找

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

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

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

  6. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

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

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

  9. [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找

    题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...

  10. leetcode——Search a 2D Matrix 二维有序数组查找(AC)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. Python大数据依赖包安装

    一.安装 先安装python2.7.6,win下的numpy这些包需要直接匹配版本,然后安装“numpy-1.8.1-win32-superpack-python2.7”和“scipy-0.16.0- ...

  2. 科学技术法转成BigDemcial

    目的:将类似“-412615050624334534247E-3”转成“-412615050624334534.247” 工具:用到BigDemcial Code: public static voi ...

  3. Orchard中文学习视频录制完成

    Orchard学习视频已登录百度传课: http://www.chuanke.com/3027295-124882.html http://pan.baidu.com/s/13zc0u 1.orcha ...

  4. [错误代码:0x80070002]IIS7及以上伪静态报错404

    故障现象:DTCMS开启伪静态功能,VS2010预览正常,发布到IIS后报错404.0错误 (WIN7,WIN8,SERVER2008).模块IISWebCore通知MapRequestHandler ...

  5. tomcat错误:@HandlesTypes annotation of one or more ServletContentInitializers

    项目在别人的机器上运行正常,但是在自己机器上运行出现该错误,所以判断应该是环境配置的问题,通过更换eclipse.更换jdk.更换maven.更换tomcat的不同版本,最终确认是tomcat的问题. ...

  6. hibernate缓存机制详细分析

    转自:http://www.cnblogs.com/xiaoluo501395377/p/3377604.html 在本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级 ...

  7. openOffice将doc在线预览

    最近,有个项目要用到类似DOCIN的文档转换和阅读的功能,于是就开始找相关的资料,最后总结出2种解决办法,以下就来探讨下两种方法的各自实现. 第一种:通过FLASH PAPER来转换DOC文档直接生成 ...

  8. LINUX下的时间与时区的设置

    在RHEL下,如果只装英文版系统,设置好时区以后(上海时间,UTC) 在命令行下用date命令查看,总是与实际的北京时间差8小时,其实硬件时间都是准确的.会带来视觉不便. 今天下决心解决此问题,不过也 ...

  9. Javascript实现 图片的无缝滚动

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. OAuth 2 的简单理解

    什么是 OAuth 2.0 根据 oauth.net 的描述,我们可以将它简述为以下内容:OAuth 2.0 是 OAuth 1.0 框架协议的升级版本,简化了多种平台上身份及授权认证的流程. 具体的 ...