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. [工作积累] Android: Hide Navigation bar 隐藏导航条

    https://developer.android.com/training/system-ui/navigation.html View decorView = getWindow().getDec ...

  2. wireshark常用的过滤命令

    我们使用wireshark抓包,却不知道如何分析这些包,也无法从海量的包中提取自己需要的数据,下面简单介绍下wireshark的过滤规则. 过滤源ip.目的ip.在wireshark的过滤规则框Fil ...

  3. Unity3D 使用 Editor 脚本,自定义 脚本的属性面板

    1. 先有一个普通的 继承自 MonoBehaviour 的脚本. 2. 创建一个 Editor 文件夹, 写 关于 UnityEditor 的脚本 都要放在这个文件夹下,不然会编译出错. 具体的实现 ...

  4. pyhton Chapter3 读文件

    使用内置函数open()打开文件,data=open("1.txt").利用data.close()关闭文件.利用data.readline()读取文件中的一行数据,然后指示读取文 ...

  5. 【C++】指针数组和数组指针

    首先的首先,稍微抱怨一下阿里今天的严重失误.说好的晚六点笔试,说好的务必提前半小时到场.六点十五的时候告诉闷在一个大教室里躁动的我们“今天七点半开考,大家先回去吧,七点半再过来”,满脸黑线…等到七点半 ...

  6. JavaScript面向对象+Array的用法及字符串组合+动态建立锚点

    脚本部分: function school(sName,sDddress,sPhone,sMail) { this.SName = sName; this.SAddress = sDddress; t ...

  7. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

  8. POJ 2013

    #include <iostream> #include <string> #define MAXN 20 using namespace std; string _m[MAX ...

  9. redis rdb

    http://blog.chinaunix.net/uid-1757778-id-3977331.html

  10. a cold welcome

    What does 'a cold welcome' refer to? On wednesday evening,we went to the town hall. It was the last ...