/**
* 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的更多相关文章

  1. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  2. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  3. [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 ...

  4. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  5. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 【一天一道LeetCode】#74. Search a 2D Matrix

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

随机推荐

  1. python 安装第三方库

    找到找到C:\Windows\System32下面的cmd.exe,   以管理员方式运行cmd.exe cd 到自己安装的python目录,这里举个例子      C:\Program Files ...

  2. 软件工程与UML第一次作业

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2/ 这个作业要求在哪里 https://edu.cnblogs.com/campus/f ...

  3. C语言printf()函数的格式化字符串

    原文链接:https://www.runoob.com/cprogramming/c-function-printf.html#include<stdio.h> #include<s ...

  4. loading爬坑--跳出思维误区

    最近在摸loading这个登录的loading动画,爬了一些坑. 第一坑--百度坑 我们爬的坑,前人都已经已经爬过了.并且把路都放在度娘了.--鲁迅 我最开始是不知道这个直接叫loading的,最开始 ...

  5. Spring Boot 2.x 多数据源配置之 JPA 篇

    场景假设:现有电商业务,商品和库存分别放在不同的库 配置数据库连接 app: datasource: first: driver-class-name: com.mysql.cj.jdbc.Drive ...

  6. 基于CefSharp开发(四)浏览器文件下载

    一.CefSharp文件下载分析 查看ChromiumWebBrowser类发现cef数据下载处理在IDownloadHandler中进行,但并未找到相应的实现类,故我们需要自己实现DownloadH ...

  7. C# 继承类的值赋

    C# 继承类的值赋 /// <summary> /// 将父类的值赋值到子类中 /// </summary> /// <typeparam name="TPar ...

  8. 题解-CF101D Castle

    题面 CF101D Castle 给一棵 \(n\) 个节点的带权树,求一种遍历方案,从 \(1\) 出发,每条边走两次,走过所有点,第一次经过每个节点的平均时间最小.输出这个平均时间. 数据范围:\ ...

  9. MySQL技术内幕InnoDB存储引擎(三)——文件相关

    构成MySQL数据库和InnoDB存储引擎表的文件类型有: 参数文件:MySQL实例运行时需要的参数就是存储在这里. 日志文件:用来记录MySQL实例对某种条件做出响应时写入的文件. socket文件 ...

  10. MySQL技术内幕InnoDB存储引擎(一)——MySQL体系结构和存储引擎

    1.数据库和实例 数据库(database)和实例(instance)不能混淆. 什么是数据库 数据库是物理操作系统文件或其他文件类型的集合.说白了,就是存储着的文件,不会运行起来,只能被实例增删改查 ...