【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 ...
随机推荐
- Windows Phone 开发起步之旅之一 平台环境的搭建
最近大家都在写博客园的技术文章,按耐不住了,也把自己平时学习中遇到和学习到的一些东西写出来,供大家分享也好,自己留个纪念也好,有个可以查看的东西. 言归正传,随着微软对Windows Phone的投入 ...
- Jquery设置select控件指定text的值为选中项
<select name="streetid" id="streetid"> <option value="4">北 ...
- 【转】java 访问.net webservice返回的数据集
转自[转的也是转的][http://blog.csdn.net/fox123871/article/details/8637839] 1. 概述 很多正在开发或者打算开发XML Web Service ...
- 取消双向绑定、输出html代码
1.取消双向绑定,在绑定的值前加*号. 如: <div id="app"> <p>{{*message}}</p> </div> 2 ...
- silverlight获取web的url参数
1.网址(如:http://localhost:8081/index.aspx?name=123) 2.获取name=123的信息 3.IDictionary<string,string> ...
- Hbase的Observer
hbase提供了类似于触发器的组件observer,类似于存储过程的endpoint. hbase中的observer分别三类,MasterObserver.RegionObserver.WALObs ...
- Hadoop在win7下部署的问题
问题: 为了测试方便所以在win7下部署了伪分布式hadoop运行环境,但是部署结束后在命令行运行hadoop命令创建一个用户文件目录时出现了一下情况: 系统找不到指定的批标签- make_comma ...
- 【积硅计划】http协议基础
http:超文本传输协议,它允许将超文本标记(html)文档从web服务器传送到浏览器.目前版本HTTP/1.1 http请求过程: proxy:代理服务器,网络信息的中转站.功能如下: ...
- The Rotation Game (POJ 2286) 题解
[问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...
- rails devise使用
gem 'devise'rails g devise:install Userrails g devise Userrails g devise:views