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.

Hide Tags

Array Binary Search

 

   在一个矩阵中确定是否存在某个数,这个矩阵有规律:一个排序好的数组,一行一行地填入矩阵。解题思路就是先二分查找会在哪一行,然后二分查找找是否存在。
#include<iostream>
#include<vector>
using namespace std; class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int m=matrix.size();
if(m<) return false;
int n=matrix[].size();
int tm;
//cout<<"m:"<<m<<" n:"<<n<<endl;
if(m==) tm=;
else{
if(matrix[m-][]<=target) tm=m-;
else{
int lft=,rgt=m-;
tm =;
do{
if(matrix[tm+][]>target) break;
int mid = (lft+rgt)/;
if(matrix[mid][]>target) rgt=mid;
else lft=mid;
tm = lft;
}while(lft+<rgt);
}
}
int lft=,rgt=n-;
if(matrix[tm][rgt]==target||matrix[tm][lft]==target) return true;
if(matrix[tm][rgt]<target) return false;
int tn=lft;
do{
int mid=(lft+rgt)/;
if(matrix[tm][mid]>target) rgt = mid;
else lft=mid;
tn = lft;
if(matrix[tm][tn]==target) return true;
}while(lft+<rgt);
return false;
}
}; int main()
{
vector<vector<int> > matrix{{, , , },{, , , },{, , , }};
Solution sol;
cout<<sol.searchMatrix(matrix,)<<endl;
return ;
}

[LeetCode] Search a 2D Matrix 二分搜索的更多相关文章

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

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

  3. LeetCode Search a 2D Matrix II

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

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

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

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

  7. [leetcode]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

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

  9. LeetCode Search a 2D Matrix II (技巧)

    题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...

随机推荐

  1. http 高级配置 虚拟主机,https 编译安装

    目录 http 高级配置 虚拟主机,https 编译安装 http 重定向 https HSTS HSTS preload list http 自带的工具程序 httpd的压力测试工具 实现状态页 反 ...

  2. ecshop里提出来的js常用函数

    目录 Utils.js jquery.listTable.js 使用例子: ecshop里提出来的js常用函数 Utils.js /* $Id : utils.js 5052 2007-02-03 1 ...

  3. oracle insert用法总结

    总结下Oracle 中的Insert用法   1.标准Insert --单表单行插入   语法:     INSERT INTO table [(column1,column2,...)] VALUE ...

  4. Problem E. TeaTree - HDU - 6430 (树的启发式合并)

    题意 有一棵树,每个节点有一个权值. 任何两个不同的节点都会把他们权值的\(gcd\)告诉他们的\(LCA\)节点.问每个节点被告诉的最大的数. 题解 第一次接触到树的启发式合并. 用一个set维护每 ...

  5. HUD:3746-Cyclic Nacklace(补齐循环节)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...

  6. Android开发——用户在屏幕上的手势识别

    个定点决定.四个属性分别为left(1),top(2),right(3),bottom(4). 数字为图上标出的距离.显然这四个属性是相对于父容器来定的,均可以通过get()方法获取. 因此很容易得出 ...

  7. c++实验4

    1. 车辆基本信息管理  #include <iostream> using namespace std; #include <string> #include "c ...

  8. NMF分解(二)

    应用: 一.图像分析 NMF最成功的一类应用是在图像的分析和处理领域.图像本身包含大量的数据,计算机一般将图像的信息按照矩阵的形式进行存放,针对图像的识别.分析和处理也是在矩阵的基础上进行的.这些特点 ...

  9. datagrid的修改和删除功能的实现

    1.修改 双击,进入一行的编辑状态的功能的实现 2.删除 3.扩展easyui的datagrid,添加动态增加或删除Editor的方法 (1)背景要求: 对于某一列,比如密码,动态增加时候,是可以编辑 ...

  10. Hyper-v Server 2012 R2增强会话模式

    从Windows Server 2012 R2开始,通过使用Hyper-v增强会话模式Hyper-v中的虚拟机允许重定向虚拟机连接会话中的本地资源.这是因为Windows Server 2012 及早 ...