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.该矩阵 ...
随机推荐
- AFNetworking https自签名证书 -1012 解决方案
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy]; //是否信任服务器无效或过期的SSL证书.默认为“不”. se ...
- visual studio各版本下载
软件包括以下几种: cn_visual_studio_2010_ultimate_x86_dvd_532347.part1.rar cn_visual_studio_2010_ultimate_x86 ...
- Stars in Your Window POJ - 2482
错误记录: 题目说输入在int范围内,但是运算过程中可能超int:后来开了很多longlong就过了 #include<cstdio> #include<algorithm> ...
- js 获取最后一个字符
方法一: str.charAt(str.length - 1) 方法二: str.subStr(str.length-1,1) 方法三: var str = "123456" ...
- C# winform 创建快捷方式
using System;using IWshRuntimeLibrary;using System.IO; namespace UavSystem.Common{ public class S ...
- markdown快捷键(转)
markdown快捷键 GLYPH NAME COMMAND Command, Cmd, Clover, (formerly) Apple ALT Option, Opt, (Windows) Alt ...
- 字符串、数组、json
一.字符串 string 1.字符串的定义: (1).var s="haha"; (2).var s=new string ("hello") 对象形式定义 2 ...
- nodejs中相互引用(循环引用)的模块分析
话不多少,直接上源码吧: modA.js: module.exports.test = 'A'; const modB = require('./05_modB'); console.log( 'mo ...
- 【学习笔记】深入理解js原型和闭包(3)——prototype原型
既typeof之后的另一位老朋友! prototype也是我们的老朋友,即使不了解的人,也应该都听过它的大名.如果它还是您的新朋友,我估计您也是javascript的新朋友. 在咱们的第一节(深入理解 ...
- 毕业设计:主界面(ViewPager + FragmentPagerAdapter)
一.主要思路 应用程序的主界面包含三个部分:顶部标题栏.底部标识栏和中间的内容部分.顶部标题栏内容基本不变,用于显示当前模块或者整个应用的名称:底部既能标识出当前Page,又能通过触发ImageBut ...