74. Search a 2D Matrix (Graph; Divide-and-Conquer)
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
.
思路:先按行二分搜索得到行号,再按列二分搜索
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int start = , end = matrix.size()-;
int mid;
int lineNum;
while(start<=end){
mid = start + ((end-start)>>);
if(matrix[mid][]<target){
start = mid+;
}
else if(matrix[mid][]>target){
end = mid-;
}
else return true;
}
if(end < ) return false;
lineNum = end;
start = ;
end = matrix[].size()-;
while(start<=end){
mid = start + ((end-start)>>); if(matrix[lineNum][mid]<target){
start = mid+;
}
else if(matrix[lineNum][mid]>target){
end = mid-; }
else return true;
}
return false;
}
};
74. Search a 2D Matrix (Graph; Divide-and-Conquer)的更多相关文章
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 74. Search a 2D Matrix
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- [LeetCode] 74. Search a 2D Matrix 解题思路
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- LeetCode 74. Search a 2D Matrix(搜索二维矩阵)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- leetcode 74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- 20155226 2016-2017-2 《Java程序设计》第8周学习总结
20155226 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 通用API 日志 java.util.logging包提供了日志功能相关类与接口,使用日志的 ...
- macOS -- 如何通过终端开启/关闭SSH
在macOS中(较新版),基本都会配置了SSH,能完成我们开发中绝大部分功能,所以不需要再去使用第三方的软件去操作. 不过SSH守护进程是默认禁用的,我们需要手动开启 1. 查看是否开始SSH功能 s ...
- PyCharm永久激活
目录 windws Mac Windows下破解 激活前准备工作 激活前请先关闭pycharm 修改配置文件的时候你需要填写你的安装路径 如果出现修改配置文件后无法打开pycharm,那就移动补丁的位 ...
- flash exe to flv swf
一般婚纱视频的文件都是用adobe软件转化为exe文件,所以只能用adobe flash打开,想上传到网上供朋友欣赏,却发现格式不对,那么我们可以用以下的方法将exe格式的视频转化为swf和flv等视 ...
- 蚂蚁金服SOFAMesh在多语言上的实践
在用一项技术前,一定要知道它的优点和缺点,它的优点是否对你有足够的吸引力,它的缺点不足你是否有办法补上.黄挺在CNUTCon全球运维大会上的分享也很不错. 黄挺,蚂蚁金服高级技术专家,蚂蚁金服分布式架 ...
- MHA配置文件说明
root@192.168.0.20 ~]# cat /etc/masterha/app1.cnf [server default] manager_workdir=/var/log/masterha/ ...
- debian8下pgsql的主备同步热切手动脚本
9以后,通过流复制直接做 主:192.168.1.111 从:192.168.1.222 需要在postgres帐号下先配置ssh互信,双机都配置 sh-keygen -t rsa ssh-copy- ...
- C++ 构造函数_内存分区_对象初始化
内存分区 栈区:int x = 0:int *p = NULL; 定义一个变量,定义一个指针时,会在栈区进行分配内存.分配的内存系统分配收回的,我们不用管. 堆区:int *p = new i ...
- Train-Alypay-Cloud:蚂蚁大数据平台培训开课通知(第三次)
ylbtech-Train-Alypay-Cloud:蚂蚁大数据平台培训开课通知(第三次) 1.返回顶部 1. 您好! 很高兴通知您,您已经成功报名将于蚂蚁金服计划在2018年2月28日- 2018年 ...
- mysql-13处理重复数据
1.防止表中出现重复数据 在mysql数据表中设置指定的字段为主键或唯一索引来保证数据的唯一行. -- 方法1:指定主键 create `table person_tbl`( `first_name` ...