【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 ...
随机推荐
- JavaScript中this详解
这里的主题是 this ,不扯远了.this 本身原本很简单,总是指向类的当前实例,this 不能赋值.这前提是说 this 不能脱离 类/对象 来说,也就是说 this 是面向对象语言里常见的一个关 ...
- C#winform设置DateTimePicker的时间格式
在对DateTimePicker进行时间格式设置时候,要先对属性Format设置为"Custom"自定义格式,然后再CustomFormat里面进行格式设置 比如"yyy ...
- Silverlight RadGridView的HeaderCellStyle样式
效果图 <UserControl x:Class="SilverlightApplication7.MainPage" xmlns="http://schemas. ...
- MHA学习笔记
MHA是一款开源的MySQL高可用程序,为MySQL主从复制架构提供了节点故障转移功能,当 master发生故障时MHA会自动提升拥有最新数据的slave节点成为新的主节点,还提供了master节 点 ...
- tomcat学习笔记2
LNMT在网站架构中的实现过程: Client --> http --> Nginx --> reverse_proxy (http) --> tomcat (http con ...
- php中iconv函数使用方法
最近在做一个程序,需要用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些. iconv函数库能够完成各种字符集 ...
- Java迷题:等于,还是不等于?
等于还是不等于? 看来看下面的一段代码: public static void main(final String[] args) { Integer a = new Integer(100); In ...
- PHP Startup: Unable to load dynamic library
昨天帮一朋友配置服务器结果发现apache日志中有PHP Warning: PHP Startup: Unable to load dynamic library 提示了,然后调试数据库连接发现提示C ...
- apache下ab.exe使用方法。。
自己在cmd中写了半天的路径也没有写对..最后网上的一个哥们告诉我说没有共同语言了...毛线啊 差距确实很大!大能猫死panda早晚干掉你,叫你丫整天嘲讽我! 比如我的ab.exe在D盘的wamp文件 ...
- SVG矢量图--爱心
aixin.xml: <!-- height:width=viewportHeight:viewportWidth --> <vector xmlns:android="h ...