【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 ...
随机推荐
- egg
简介 阿里的 红色的是项目名字 egg-init --type simple First && cd First egg-init --type simple Second &a ...
- Linux - 组管理和权限管理
l Linux组基本介绍 在linux中的每个用户必须属于一个组,不能独立于组外.在linux中每个文件有所有者.所在组.其它组的概念. 1) 所有者 2) 所在组 3) 其它组 4) 改变用户所在的 ...
- js函数声明提升与变量提升
变量提升 变量提升: 在指定作用域里,从代码顺序上看是变量先使用后声明,但运行时变量的 “可访问性” 提升到当前作用域的顶部,其值为 undefined ,没有 “可用性”. alert(a); // ...
- table定位
Table定位 在 web 页面中经常会遇到 table 表格,特别是后台操作页面比较常见.本篇详细讲解 table 表格如何定位. 1.1 table特性 1.table 页面查看源码一般有这几个明 ...
- Mysql远程连接配置
Mysql远程连接配置 环境:unbuntu 16.04 最新版本的Mysql在远程连接的配置上与老版本有了一些出入,照原先的配置已经不行了,所以在这里记录一下遇到的所有新问题. 配置远程连接的步骤如 ...
- django允许跨域请求配置
django允许跨域请求配置 下载corsheader pip install django-cors-headers 修改setting.py中配置 在INSTALLED_APPS中增加corshe ...
- Win32 DPAPI加密编程
DPAPI函数是CryptoAPI中少有的简单易用的加密函数,调用过程简单,其调用接口几乎不涉及密码学概念.Win32 DPAPI有4个函数,它们分别是CryptProtectData.CryptUn ...
- [译文和个人分析]REST vs RPC - RESTful究竟是什么?
一 好烦啊,分不清REST RPC RESTful的区别,所以只能翻译一篇谷歌的文章,括号中是我的补充 原文连接 REST vs RPC - What is RESTful? 注意需要*** 二 译文 ...
- Silverlight & Blend动画设计系列三:缩放动画(ScaleTransform)
在Silverlight的动画框架中,ScaleTransform类提供了在二维空间中的坐标内进行缩放操作,通过ScaleTransform可以在水平或垂直方向的缩放和拉伸对象,以实现一个简单的缩放动 ...
- 项目开发-->高级功能汇总
祭奠曾经逝去的青春…… 1.高级功能汇总-->Memcached之ASP.NET实现 2.高级功能汇总-->HubbleDotNet软件安装