题目

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. js页面字段的必填验证方法

    https://blog.csdn.net/fn_2015/article/details/73498462 <script type="text/javascript" s ...

  2. bryce1010专题训练——树状数组

    Bryce1010模板 1.一维树状数组 https://vjudge.net/contest/239647#problem/A[HDU1556] #include<bits/stdc++.h& ...

  3. 优先队列 POJ 3253 Fence Repair

    题目传送门 题意:一块木板按照某个顺序切成a[1], a[2]...a[n]的长度,每次切都会加上该两段木板的长度,问选择什么顺序切能使得累加和最小 分析:网上说这是哈夫曼树.很容易想到先切掉最长的, ...

  4. sql 语句操作,修改字段中字符串的一部分

    update 表名 set 字段=replace(字段,‘替换的部分’,‘替换后的字符串’): update 表名 set A=replace( A, '海淀', '朝阳') where A like ...

  5. 系统中同时存在python2和python3时 pip有时候更新后会报错 解决安装的方法如下

    官网原链接:https://pip.pypa.io/en/stable/installing/ Installation Do I need to install pip? pip is alread ...

  6. Node.Js的Module System 以及一些常用 Module

    Node.Js学习就按照这本书的流程来. 在第7章结束与第10章结束时分别自己出一个小项目练练手.Node.Js的入门学习计划是这样. 目录:, QQ:1045642972 欢迎来索书以及讨论Node ...

  7. 洛谷P1251 餐巾计划问题(最小费用最大流)

    题意 一家餐厅,第$i$天需要$r_i$块餐巾,每天获取餐巾有三种途径 1.以$p$的费用买 2.以$f$的费用送到快洗部,并在$m$天后取出 3.以$s$的费用送到慢洗部,并在$n$天后取出 问满足 ...

  8. jQuery中面向对象思想实现盒子内容切换

    这里主要是模拟小米官网中的首页的内容模块实现的主要动态效果 布局:采用了bootstrap框架进行布局,及其其中的字体图标 html: <!-- 内容 --> <div class= ...

  9. Java网络编程学习笔记

    Java网络编程,我们先来看下面这一张图: 由图可得:想要进行网络编程,首先是服务器端通过ServerSocket对某一个端口进行监听.通过accept来判断是否有客户端与其相连.若成功连上,则通过r ...

  10. iOS 创建xcode插件

    苹果的"一个足以应付所有"策略使得它的产品越来越像一个难以下咽的药丸.尽管苹果已经将一些工作流带给了iOS/OS X的开发者,我们仍然希望通过插件来使得Xcode更加顺手! 虽然苹 ...