LeetCode(74) Search a 2D Matrix
题目
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.
分析
二分搜索算法在二维矩阵中的应用;
两次二分搜索:
第一次,二分搜索第一列(借助另一个数组)找出小于等于target的下标pos
第二次,二分搜索pos行,搜索target
AC代码
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty())
return false;
int m = matrix.size();
//将矩阵首列,压入临时vector,寻找,目标元素所在的行
vector<int> v;
for (int i = 0; i < m; i++)
v.push_back(matrix[i][0]);
//寻找 <=target 最近的元素下标
int pos = binarySearchPos(v, 0, m - 1, target);
//不存在任何一行中
if (pos == -1)
return false;
else if(matrix[pos][0] == target)
return true;
else
return binarySearch(matrix[pos], 0, matrix[pos].size() - 1, target);
}
int binarySearchPos(vector<int> &nums, int lhs, int rhs, int &target)
{
if (nums.empty())
return -1;
while (lhs <= rhs)
{
int mid = (lhs + rhs) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
{
lhs = mid + 1;
}
else{
rhs = mid - 1;
}//else
}//while
if (lhs < rhs && nums[lhs] < target)
{
return lhs;
}
else{
return lhs - 1;
}
}
bool binarySearch(vector<int> &nums, int lhs, int rhs, int &target)
{
if (nums.empty())
return false;
while (lhs <= rhs)
{
int mid = (lhs + rhs) / 2;
if (nums[mid] == target)
return true;
else if (nums[mid] < target)
{
lhs = mid + 1;
}
else{
rhs = mid - 1;
}//else
}//while
return false;
}
};
LeetCode(74) Search a 2D Matrix的更多相关文章
- (LeetCode74)Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode(74):搜索二维矩阵
Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 ...
- LeetCode(81) Search in Rotated Array II
题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- LeetCode(33)Search in Rotated Sorted Array
题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...
- LeetCode(35) Search Insert Position
题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode(34)Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
随机推荐
- jQuery笔记之工具方法—高级方法Ajax
$.ajxa() ——基本使用 前提:先了解js的执行机制 $.Callbacks()——回调 $.Deferred()——异步 $.when() 网络服务器链接由<渡一教育>提供 --- ...
- 使用Spring MVC的@RequestBody注解接收Json对象字符串
最近公司在开发移动APP,APP上通过jQuery提交表单的json字符串格式数据到Java后端,之前通过request手动接收,非常麻烦,其实Spring MVC已经为我们提供了一个注解@Reque ...
- Hexo瞎折腾系列(1) - 准备工作与简单美化
前言 网上有不少相关的帖子,不过版本会比较旧,而不同版本可能存在代码不同的问题,不过大部分还是大同小异,本系列就不啰嗦重复了,基本只会按照本人所使用的版本以及个人所使用到的内容来进行介绍. 该系列是对 ...
- c++,类的对象作为形参时一定会调用复制构造函数吗?
c++,类的对象作为形参时一定会调用复制构造函数吗? 答:如果参数是引用传递,则不会调用任何构造函数:如果是按值传递,则调用复制构造函数,按参数的值构造一个临时对象,这个临时对象仅仅在函数执行是存在, ...
- CSS选择器优先级【转】
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...
- AtCoder Regular Contest 062 E - AtCoDeerくんと立方体づくり / Building Cubes with AtCoDeer
题目传送门:https://arc062.contest.atcoder.jp/tasks/arc062_c 题目大意: 给你\(N\)块正方形木板,每块木板四角有四种颜色(可以相同),木板中央有编号 ...
- 【bzoj2084】[Poi2010]Antisymmetry
2084: [Poi2010]Antisymmetry Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1205 Solved: 756[Submit ...
- Lucky Array Codeforces - 121E && Bear and Bad Powers of 42 Codeforces - 679E
http://codeforces.com/contest/121/problem/E 话说这题貌似暴力可A啊... 正解是想出来了,结果重构代码,调了不知道多久才A 错误记录: 1.线段树搞混num ...
- 莫比乌斯函数 && HDU-1695
莫比乌斯函数定义: $$\mu(d)=\begin{cases}1 &\text{d = 1}\\(-1)^r &\text{$d=p_1p_2...p_r,其中p_i为不同的素数$} ...
- springMVC的架构与执行流程
SpringMVC术语 前端控制器(DispatcherServlet):接收请求,响应结果,相当于电脑的CPU. 处理器映射器(HandlerMapping):根据URL去查找处理器 处理器(Han ...