【Leetcode】【Medium】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.
解题思路:
在有序队列中寻找目标值,典型的二分搜索应用。
可以有两种思路:(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的更多相关文章
- [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 ...
- 【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 ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【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 ...
- 【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 ...
- 【刷题-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 ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 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 ...
随机推荐
- JQ 输入框控制输入 - 键盘上事件
<li style="width: 480px">限购数量 <input type="number" name="limitNum ...
- selenium+Python(alert 、confirm 、prompt 的处理)
alert\confirm\prompt 弹出框操作主要方法有: text 返回 alert/confirm/prompt 中的文字信息 accept 点击确认按钮 dismiss 点击取消按钮, ...
- spring-session-data-redis使用redis共享session
1:添加jar包 <dependency> <groupId>org.springframework.data</groupId> <artifactId&g ...
- java实现任务调度
最近的一个小项目是做一个简单的数据仓库,需要将其他数据库的数据抽取出来,并通过而出抽取成页面需要的数据,以空间换时间的方式,让后端报表查询更快. 因为在抽取的过程中,有一定的先后顺序,需要做一个任务调 ...
- Linux 命令 -- chown
chown命令改变某个文件或目录的所有者和所属的组,该命令可以向某个用户授权,使该用户变成指定文件的所有者或者改变文件所属的组.用户可以是用户或者是用户D,用户组可以是组名或组id.文件名可以使由空格 ...
- 学习笔记之--认识Xcode中的重要成员:lldb调试器
之前对lldb调试器了解比较少,平时主要用来打印日志和暂定时用鼠标查看属性数据以及使用p po一些简单的命令语句. 今天看了一些关于lldb的文章,顿时觉得之前对它了解太少了,原来它还有那么多的功能. ...
- CMS gc随记
在查看CMS相关中文资料时,都提到了 并发预清理(Concurrent precleaning) 重新标记(STW remark) 目的是重新标记在并发标记阶段,由于对象状态的改变而标记遗漏的对象. ...
- linux压缩和解压缩命令大全[转]
.tar 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName ------------------------------------- ...
- CSS基础知识---浮动,定位和盒模型
转载请注明出处! 需要掌握的三个最重要的CSS概念是浮动,定位和盒模型. 盒模型概述: 页面上的每个元素都被看做一个矩形框(元素框or盒模型),这个框由元素内容,内边距,边框和外边距组成. 内边距出现 ...
- python中的单例模式的应用
1 使用__new__方法 class Singleton(object): def __new__(cls, *args, **kw): if not hasattr(cls, ...