[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 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.
*/
/*
* 思路是先找到target所在的行,然后在这一行找有没有,找寻的方式都是二分查找*/
public class Q74Searcha2DMatrix {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0)
return false;
int sta = 0;
int fin = matrix.length,mid=0;
boolean res = false;
//二分法查找行
while(sta < fin)
{
mid = (sta + fin)/2;
if (matrix[mid][0] <= target)
{
//如果这一行开头小于等于target且下一行开头大于它(或这一行就是最后一行)
if ((mid + 1) >= matrix.length || matrix[mid+1][0] > target)
break;
else
sta++;
}
else
{
fin--;
}
}
int row = mid;
sta = 0;
fin = matrix[0].length;
//二分法查找数据
while (sta < fin)
{
mid = (sta + fin)/2;
if (matrix[row][mid] == target)
{
res = true;
break;
}
else if (matrix[row][mid] > target)
{
fin--;
}
else
{
sta++;
}
}
return res;
}
}
[leetcode] Add to List 74. Search a 2D Matrix的更多相关文章
- [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/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 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 解题思路
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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
随机推荐
- 自动化运维工具之Puppet常用资源(二)
前文我们了解了部分puppet的资源的使用,以及资源和资源的依赖关系的定义,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/14071459.html:今天我们继 ...
- 【佛山市选2013】JZOJ2020年8月7日提高组T1 回文子序列
[佛山市选2013]JZOJ2020年8月7日提高组T1 回文子序列 题目 描述 回文序列是指左右对称的序列.例如1 2 3 2 1是回文序列,但是1 2 3 2 2就不是.我们会给定一个N×M的矩阵 ...
- PyQt(Python+Qt)学习随笔:QTreeWidgetItem项列图标的访问方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件QTreeWidget中的QTreeWidgetItem项中可以有多列数据,每列数据都可以设 ...
- PyQt(Python+Qt)学习随笔:QListView的spacing属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListView的spacing属性用于控制视图布局中数据项周围填充的空白空间的大小.缺省值为0, ...
- PyQt(Python+Qt)入门:Designer组件属性编辑界面中QWidget类相关属性详解
本文适用人员:本文比较长,适合不理解Qt Designer部件属性的人员阅读或资料查找. 声明: 1.如果有人认为本文是简单的复制粘贴+翻译而成,敬请读本文最后的后记: 2.本文为老猿Python学习 ...
- 「Elasticsearch」SpringBoot快速集成ES
Elastic Search 的底层是开源库 Lucene.但是Lucene的使用门槛比较高,必须自己写代码去调用它的接口.而Elastic Search的出现正是为了解决了这个问题,它是 Lucen ...
- linux进程管理(linux命令安装、进程生命周期、进程状态)
1 linux下如何杀掉进程 1)找到包名所占用的端口: ps aux | grep cbs_portal-1.0.1.jar(包名) 2)杀掉进程: kill 10942(端口号) PS: //-- ...
- "迎圣诞,拿大奖"赛题——SQLI
0x01 本题所需知识清单: 1.php sprintf()函数漏洞:https://blog.csdn.net/WQ_BCJ/article/details/85057447 2.布尔盲注基本pla ...
- 归并排序(c++,递归)
放上c++代码模板(但是该版本中,还可以再进一步优化成原地算法,即不开辟新的空间:本代码中空间复杂度为n,不是1) 1 #include <iostream> 2 #include< ...
- vue之keep-alive组件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...