题目

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;
}
};

GitHub测试程序源码

LeetCode(74) Search a 2D Matrix的更多相关文章

  1. (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 ...

  2. LeetCode(74):搜索二维矩阵

    Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  3. LeetCode(81) Search in Rotated Array II

    题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...

  4. 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 ...

  5. LeetCode(35) Search Insert Position

    题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  6. LeetCode(34)Search for a Range

    题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...

  7. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  8. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  9. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

随机推荐

  1. 学习JavaScript数据结构与算法 (二)

    学习JavaScript数据结构与算法 的笔记 包含第四章队列, 第五章链表 本人所有文章首发在博客园: http://www.cnblogs.com/zhangrunhao/ 04队列 实现基本队列 ...

  2. tsconfig.json No inputs were found in config file

    Build:No inputs were found in config file '/tsconfig.json'. Specified 'include' paths were '["* ...

  3. 495 Teemo Attacking 提莫攻击

    在<英雄联盟>的世界中,有一个叫“提莫”的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和提莫攻击的中毒持续时间,你需要输出艾希的中毒 ...

  4. 通过流传入excel解析的问题

    做了个excel文件导入的功能,接收excel文件流,先读取文件流头部信息确定文件类型,然后调用poi方法 OPCPackage pkg = OPCPackage.open(inputStream) ...

  5. 【转】javap -c命令详解

    javap -c命令详解 一直在学习Java,碰到了很多问题,碰到了很多关于i++和++i的难题,以及最经典的String str = "abc" 共创建了几个对象的疑难杂症. 知 ...

  6. H+后台主题UI框架---整理(二)

    本篇文章是针对H+后台主题UI框架的整理的第二部分.主要只有一个point.如下: 其代码如下: <div class="ibox float-e-margins"> ...

  7. Spring-bean(零)

    内容提要:红为1,黄2,绿3 -----配置形式:基于xml文件的方式:基于注解的方式 -----Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),Facto ...

  8. VC++编译出错:LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    解决方法: 1.搜索C盘下的cvtres.exe,结果得到类似这样的列表: C:\Program Files\Microsoft Visual Studio 10.0\VC\bin C:\Window ...

  9. Implicit Animations 默认动画 读书笔记

    Implicit Animations  默认动画 读书笔记 Do what I mean, not what I say. Edna Krabappel, The Simpsons   Part I ...

  10. asp.net mvc 5 微信接入VB版 - 获取AccessToken

    获取AccessToken是微信接入的又一个基础操作.很多微信接口需要这个2小时一刷新的AccessToken作为参数. 转载请说明作者Nukepayload2 首先根据开发文档把获取AccessTo ...