【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 ...
随机推荐
- vue中遇到的坑keep-alive、vue-router相关
在进入详情页之后,然后返回到首页,报错如下. 虽说是报错了,但是对我最后的页面并没有什么影响,但是出现了一堆红色的报错,作为一个前端工程师,看着还是十分难受的!! 一旦出现问题,我的解决思路一般是, ...
- php添加mysql.so扩展
1.软件包安装 yum install php-mysql 安装的是mariadb的扩展 yum install php-mysqlnd 安装的是mysql的扩展
- md5码加密(Python)
import hashlib import hmac m = input('输入要加密内容:') md = hashlib.md5()#生成md5 hash对象 md.update(m.encode( ...
- django notes 二:URL dispatcher
一般在 settings.py 中会有一个 ROOT_URLCONF ,请求到来时 django 会从 ROOT_URLCONF 指向的文件中查找 urlpatterns 变量配置的路由. url ...
- 权重平等分布局And TableRow布局误区
开头语: 本人最近在自学Android,虽然本人有2年Java Web的开发经验.但是发现Android的自学之路并不是那么平坦,我没有Android真机.但是有一个window phone的手机.开 ...
- poj 1595 Prime Cuts
Prime Cuts Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10610 Accepted: 4046 Descr ...
- 以前折腾的7zip图标
7z.dll替换文件 http://files.cnblogs.com/colben/7zDll.7z
- 模拟Springboot一:(零xml配置搭建SSM项目)
在spring官网文档中无论是spring的基础文档,还是spring-mvc文档都推荐我们使用javaconfig的方式来搭建项目 间接说明 (优点:javaconfig配置>xml配置) 其 ...
- 前端(二):css样式
本节笔记根据css中文手册整理,内容已做成思维导图.下载地址https://files.cnblogs.com/files/kuaizifeng/css.xmind.zip. css(Csacadin ...
- Filter的常见应用
1.字符编码过滤器 实现功能,在a.jsp中填写用户名提交到b.jsp,在b.jsp中读取参数名. a.jsp <body> <form action="encoding/ ...