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. 微信小程序中 this.setData is not a function报错

    在微信小程序中我们一般通过以下方式来修改data中的数据: 比如获取小程序缓存: wx.getStorage({ key: 'is_screen', success: function (res) { ...

  2. JZOJ 5777. 【NOIP2008模拟】小x玩游戏

    5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms  Memory Limits ...

  3. python 2.7版本解决TypeError: 'encoding' is an invalid keyword argument for this function

    今天在用yaml处理数据时,由于yaml.load可接收一个byte字符串,unicode字符串,打开的二进制文件或文本文件对象,但字节字符串和文件必须是utf-8,utf-16-be或utf-16- ...

  4. 使用WMI Filter 实现组策略的筛选!

    今天接到一个客户的一个问题,提到需要分系统版本分发相应的MSI程序.比如简体版接受简体版的分发程序,繁体版接受繁体版的分发程序!这个建立组策略的不同版本分发本身不会太难,我们只需要建立两个不同组策略分 ...

  5. leetcode 【 Subsets II 】python 实现

    题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...

  6. 嵌入式tcpip

    嵌入式tcpip方案 目前高端一点的嵌入式处理器,如STM32F107,都带有MAC,因此用户在实现网络功能的时候,只需要外界PHY层的芯片, 目前使用比较都的是DM9161A.网上的驱动也比较多,开 ...

  7. vim中插入递增数

    假设生成0-9的递增数 1.插入数字1,yy复制,9p 2.输入命令 let i= | g//s//\=i/ | let i=i+1 3.结果:

  8. rule.xml属性概念

    # tableRule <tableRule name="rule1"> <rule> <columns>id</columns> ...

  9. 详解 MySQL 的计划任务

    注意:5.1以后才支持! 让MYSQL定期执行指定的一条命令.功能类似于crontab. 1. 检查你的MYSQL是否开了这个功能 SHOW VARIABLES LIKE 'event_schedul ...

  10. 自制wifi信号放大器

    自制wifi信号放大器 只要家里安装了一台无线路由器,在家里的任何地方都可以使用带上网功能的电子产品上网,但是由于距离的问题,WiFi信号有强弱之分,离无线路由器稍微远点,信号就有所降低,上网速度受影 ...