【Search a 2D Matrix】cpp
题目:
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的更多相关文章
- 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 ...
- 【Search for a Range】cpp
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 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 ...
- 【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 ...
- 【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 ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【刷题-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 ...
- [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 ...
随机推荐
- Centos6.5系统初学者基本系统配置1
作为一个初步接触linux-Centos的菜鸟来说,Centos在基本软件安装是一件比较令人头疼的事情,下面我将我初步使用linux的一些问题进行了汇总和记录,希望对大家有所帮助,这些东西也是在网友的 ...
- 字符串匹配KMP算法
1. 字符串匹配的KMP算法 2. KMP算法详解 3. 从头到尾彻底理解KMP
- SVN命令收集
1.检出 svn co http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码 svn co svn://路径(目录或文件的全路径 ...
- jQuery实现动态添加和删除一个div
本文主要给大家简单介绍一下如何动态的在一个元素添加和删除div,希望能够得到举一反三之效. 代码实例如下: <!DOCTYPE html> <html> <head> ...
- C# 和 Unix 时间戳转换
unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒. /// 时间戳转为C#格式时间 private DateTime GetTime(string timeSt ...
- LevelDB源码之五Current文件\Manifest文件\版本信息
版本信息有什么用?先来简要说明三个类的具体用途: Version:代表了某一时刻的数据库版本信息,版本信息的主要内容是当前各个Level的SSTable数据文件列表. VersionSet:维护了一份 ...
- js对象与this指向
创建对象的方法 1.对象字面量法 var obj={} var obj={ 'first-name':'Tom', 'last-name':'bush', age:24, Family:{ Broth ...
- java8新特性笔记
1.forEach(),遍历数据结构中的元素,括号内可以带一个闭包的方法 2.双冒号用法:forEach(this::doSchedule),如果运行环境是闭包,java允许使用双冒号的写法来直接调用 ...
- Qt5 Addin 出现问题模块计算机类型“x64”与目标计算机类型“X86”冲突
Qt5 Addin 出现问题 怎样VS2013下安装Qt5的插件 http://jingyan.baidu.com/article/a948d65159d8890a2dcd2e84.html ...
- 生成不重复随机数,int转 TCHAR 打印输出
在0~n 中 随机去除不重复的k个数 int k=100; int n=80000; for(int i=0;k>0&&i<n;i++) { if((bigrand()%( ...