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.

解题思路:

在有序队列中寻找目标值,典型的二分搜索应用。

可以有两种思路:(1)使用两次二分搜索,先二分搜索列,确定列之后再二分搜索行;(2)将所有元素看成一列有序数列,每个元素的下标是 row*n + col,这样只需使用一次二分搜索;

虽然第二种思路只使用一次二分搜索,代码简介,但是使用第一种思路更好:

1、方法2需要过多的“/”和“%”运算,大大降低了性能;

2、方法2对比方法1,时间复杂度没有提高;

3、由于所有元素标注为0~m*n,相比方法1先在n中二分搜索,再在m中二分搜索,方法2的做法更可能导致整数越界;

因此,选择使用方法1.

代码:

 class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int first = ;
int last = matrix.size() - ;
while (first < last) {
int mid = (first + last) / + ;
if (matrix[mid][] > target)
last = mid - ;
else
first = mid;
} if (matrix[first][] > target)
return false; int row = first;
first = ;
last = matrix[row].size() - ;
while (first < last) {
int mid = (first + last) / ;
if (matrix[row][mid] > target)
last = mid;
else if (matrix[row][mid] == target)
return true;
else
first = mid + ;
} return (matrix[row][first] == target);
}
};

【Leetcode】【Medium】Search a 2D Matrix的更多相关文章

  1. [leetcode] Add to List 74. Search a 2D Matrix

    /** * Created by lvhao on 2017/8/1. * Write an efficient algorithm that searches for a value in an m ...

  2. 【LeetCode题意分析&解答】35. Search Insert Position

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

  3. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

  5. 【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 ...

  6. 【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 ...

  7. 【刷题-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 ...

  8. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

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

随机推荐

  1. cucumber & selenium & bddtest

    目录 1.Cucumber介绍 refer link: 2.使用步骤 2.1 引入依赖 2.2新建test.future文件 2.3在resource目录下创建cucumber.bat,执行bat,c ...

  2. CMS: DNN And Umbraco

    在比较了众多CMS系统后,还是把焦点定在DNN和Umbraco两个系统上,这两个系统都可以使用UserControl扩展自己需要的功能. DNN的架构比较复杂,Module.Skin.Containe ...

  3. jQuery validate 设置失去焦点就校验

    <script type="text/javascript"> $(function(){ var flag = $("#addForm").val ...

  4. 九度oj题目1019:简单计算器

    题目1019:简单计算器 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6346 解决:2334 题目描述:     读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达 ...

  5. Android OpenGL教程-第二课【转】

    第二课 你的第一个多边形: 在第一个教程的基础上,我们添加了一个三角形和一个四边形.也许你认为这很简单,但你已经迈出了一大步,要知道任何在OpenGL中绘制的模型都会被分解为这两种简单的图形. 读完了 ...

  6. C# WinForm API 改进单实例运行

    在普通的单实例中,第二次点击软件快捷方式的时候,往往简单提示"系统已经运行",而不是把第一次打开的软件主窗体显示出来,下面演示如果主窗体已经打开则把第一次打开的主窗体放置到最前面; ...

  7. 快速清除SQL2008日志文件

    USE [master] --把数据库调整为简单模式 GO ALTER DATABASE krisvision SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DA ...

  8. sql创建表变量,转百分数

    declare @tab table( ID nt identity(1,1) primary key, --从1开始,每次自增1 ,Name nvarchar(200) ) declare a fl ...

  9. 提交代码至coding.net

    1.首先在本地任意目录下创建项目 2.cmd切换至该目录下,然后依次输入: git init git add . git commit -m "version 3.9" git r ...

  10. meteor框架学习

    meteor js的全栈开发框架,官方的解释: Meteor makes it an order of magnitude simpler, and a lot more fun. You can b ...