题目:

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. 使用C#中JavaScriptSerializer类将对象转换为Json格式数据

    将对象转换为json格式字符串: private JavaScriptSerializer serializer = new JavaScriptSerializer(); protected voi ...

  2. Linux Shell脚本编程的注意事项

    Linux下(Shell脚本 http://www.jbxue.com/jb/shell/)编程的一些注意事项,如编程风格.命名风格等. 一.常用技巧 ssh user@server bash < ...

  3. mysql 排重查询

    GROUP BY 语句可以实现某一列的去重查询. 直接上语句: select io_dev_id from io_info where (TID=1 AND host_name='yang1') GR ...

  4. php spl

    最近在重构后台,自写rbac,发现自己在设计模式方面尤为欠缺,没有一个长远的目光,所以打算静下心来看一看自己平时不关注的功能,spl就是其中之一. spl是Standard PHP Library(P ...

  5. fancybox去除不受待见的水平滚动条

    用fancybox在嵌套某个页面时,有时莫名其妙的会出现的消除不掉的幽灵般水平滚动条,如何去除: github上的解决方案:https://github.com/fancyapps/fancyBox/ ...

  6. Oracle获取表结构信息:表名、是否视图、字段名、类型、长度、非空、主键

    select a.TABLE_NAME as "TableName", then 'V' else 'U'end as "TableType", a.COLUM ...

  7. hadoop-cdh with snappy

    hadoop: 2.5.0-cdh5.3.6 snappy: 1.1.3 hadoop 2.*不需要hadoop-snappy.只要机器上安装好snappy, 直接编译就可以 编译命令: mvn cl ...

  8. 决策树的基本ID3算法

    一  ID3算法的大致思想 基本的ID3算法是通过自顶向下构造决策树来进行学习的.我们首先思考的是树的构造从哪里开始,这就涉及到选择属性进行树的构造了,那么怎样选择属性呢?为了解决这个问题,我们使用统 ...

  9. 创建表 添加主键 添加列常用SQL语句

    --删除主键 alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段 ...

  10. DB2批处理数据导入

    这里需要两个BAT文件 first.bat @echo off @set /p databaseName=1)请输入数据库名: @set /p userName=2)请输入用户名: @set /p u ...