题目:

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.

代码:

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.size()==) return false;
const int ROW = matrix.size();
const int COL = matrix[].size();
// search the target row
int begin = ;
int end = ROW-;
while ( begin<=end )
{
int mid = (begin+end)/;
int lower = matrix[mid][];
int upper = matrix[mid][COL-];
if ( target>=lower && target<=upper )
{
return Solution::binarySearch(matrix[mid], target);
}
if ( target<lower )
{
end = mid-;
continue;
}
if ( target>upper )
{
begin = mid+;
continue;
}
}
return false;
}
static bool binarySearch(vector<int>& row, int target)
{
int begin = ;
int end = row.size()-;
while ( begin<=end )
{
int mid = (begin+end)/;
if ( row[mid]==target ) return true;
if ( row[mid]>target )
{
end = mid-;
}
else
{
begin = mid+;
}
}
return false;
}
};

tips:

1. 首先二分查找可能所在的行

2. 确定某一行之后,再二分查找所在的列

完毕。

======================================

另一种思路:把大的二维矩阵当成一个一维数组看,只需要执行一次二分查找就OK了。

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.size()==) return false;
const int ROW = matrix.size();
const int COL = matrix[].size();
int begin = ;
int end = ROW*COL-;
while ( begin<=end ){
int mid = (begin+end)/;
int val = matrix[mid/COL][mid%COL];
if ( val==target ) return true;
if ( val>target ){
end = mid-;
}
else{
begin = mid+;
}
}
return false;
}
};

tips:

注意这条语句“int val = matrix[mid/COL][mid%COL]”。

一开始写成了"int val = matrix[mid/ROW][mid%COL]" 掉入了这个思维陷阱,因为每行有多少列应该是基础长度单元,所以不论是取商还是余数,分母上都应该是COL。

============================================

第二次过这道题,沿用一般的思路,先找可能在哪一行;再去行里面找。

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
// search for row
int row = -;
int begin = ;
int end = matrix.size()-;
while ( begin<=end )
{
int mid = (begin+end)/;
if ( matrix[mid][]<=target && matrix[mid][matrix[mid].size()-]>=target )
{
row = mid;
break;
}
else if ( matrix[mid][]>target )
{
end = mid-;
}
else
{
begin = mid+;
}
}
if ( row==- ) return false;
// search in the row
begin = ;
end = matrix[row].size()-;
while ( begin<=end )
{
int mid = (begin+end)/;
if ( matrix[row][mid]==target ) return true;
if ( matrix[row][mid]>target )
{
end = mid-;
}
else
{
begin = mid+;
}
}
return false;
}
};

【Search a 2D Matrix】cpp的更多相关文章

  1. leetcode 【Search a 2D Matrix 】python 实现

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

  2. 【Search for a Range】cpp

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

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

  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】240. 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. Thi ...

  6. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  8. 【刷题-LeetCode】240. 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. Thi ...

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

随机推荐

  1. 结合Git实现Mysql差异备份,可用于生产环境

    埋头苦干多年一直没写过文章,今天突发狂想,为LNMP阵营贡献一些力量.就从平时工作过程中的心得和一些技巧分享出来.今天就猿们最熟悉的Mysql开始宅鸟的开篇博客文章.欢迎猿们拍砖.转载. 注意:宅鸟的 ...

  2. win2003以isapi的方式配置php+mysql环境(安装了shopEX)

    一.准备相关组件 mysql-installer-community-5.5.29.0.zip php-5.2.17-Win32-VC6-x86 ZendOptimizer-3.3.3-Windows ...

  3. Hbase负载均衡流程以及源码

    hmater负责把region均匀到各个region server .hmaster中有一个线程任务是专门处理负责均衡的,默认每隔5分钟执行一次. 每次负载均衡操作可以分为两步: 生成负载均衡计划表 ...

  4. Nginx Location匹配举例

    1.location / {       if (!-f $request_filename){              rewrite ^/(.+)$ /uri.php last;      }} ...

  5. Dll学习二_Dll 窗体中动态创建数据并使用Demo

    沿用上一篇Demo 环境:DelphiXE,XP,SQL2005 贴出改动过的单元代码: dbGrid控件版: unit SubMain_Unit; interface uses Windows, M ...

  6. delphi 基础之二 面向对象概念初步

    面向对象概念初步 •类自动生成 快捷键:ctrl+shift+c 1.类的定义 类是用户创建的数据类型,包括状态.表达式和一些操作.有3个组成部分,即字段.方法和属性.字段是类的内部数据变量,方法就是 ...

  7. 使用Android Studio开发J2SE项目方法

    0.前言 最近因为要为项目开发一个底层的Java应用,所以非常偶然的遇到了这样一个问题,过去Eclipse有Java Project而现在手头使用Android Studio并不能直接建立Java应用 ...

  8. equals方法,hashcode()方法

    Object类的equals 方法 用来检测两个对象是否相等,即两个对象的内容是否相等,区分大小写.   (一)说到equals方法,不得不提一下==号. ==用于比较引用和比较原生数据类型时具有不同 ...

  9. ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(四)

    这是第四步点击保存将信息存入数据库中. 这个就简单了利用ajax将JSON字符串传到后台然后这里有个知识点就是将DataTable直接存入数据库中.代码如下: 一.界面获取数据JS代码: //保存订单 ...

  10. 苏泊尔借助微软CRM提升客户满意度

    企业背景 作为中国最大.全球第二的炊具研发制造商和中国小家电领先品牌,品质和创新一是苏泊尔矢志追求的企业理念,从火红点无油烟锅的发明到能做柴火饭的球釜IH饭煲的面世,苏泊尔用产品的创新和品质的承诺,不 ...