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. 最坏时间复杂度为O(m+n)的方法

分析矩阵我们发现矩阵有一个特点:它的每一行是递增的,而且每一列也是递增的。我们从矩阵的坐下角出发,如果目标值比当前值大,列索引就加1,如果目标值比当前值小,那么横索引就加1,直到搜索到目标值或者搜索完整个矩阵。例如我们要从如下矩阵中寻找目标值9:

<-----这个矩阵的每一行的第一个数并不比上一行最后一个数大,但是这个算法依旧可以应用在本题目。

我们从18出发,绿色线条显示了我们的移动轨迹。可以很容易看出,如果目标值为右上角的元素,则需要搜索最长的步数。

2. 时间复杂度为log(m) + log(n)的算法

很明显我们可以采用二分查找算法查找第一列,定位出目标值位于哪一行,然后在该行再采用二分查找具体确定目标值的位置。


【思路1代码】

 public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null) return false;
int i = matrix.length - 1;
int j = 0; while(i >= 0 && j < matrix[0].length){
if(target > matrix[i][j]) j++;
else if(target < matrix[i][j]) i--;
else return true;
}
return false;
}
}

LeetCode OJ 74. Search a 2D Matrix的更多相关文章

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

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

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

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

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

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

  4. LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. LeetCode OJ:Search a 2D Matrix(二维数组查找)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  6. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

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

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

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

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

  9. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. webservice(二)

    ---摘自网络,感谢提供者 WsExplorer和Tcp/Ip Monitor工具本身就存在于eclipse和MyEclipse中 使用工具的原因: 1.  使用工具可以更好的了解WebService ...

  2. Ubuntu环境openresty的安装

    Ubuntu环境openresty的安装 相关库的安装 安装openresty需要的库  apt-get install libreadline-dev libncurses5-dev libpcre ...

  3. swift3 控件创建

    //MARK:- UIScrollView let scrollView = UIScrollView() scrollView.delegate = target scrollView.backgr ...

  4. 设计模式 -- 中介者设计模式 (Mediator Pattern)

    中介者模式的定义:将多对多关系分散为一对多的关系,将网状结构变成星状结构,降低复杂度,提高扩展性. 角色: Mediator :抽象中介者角色,以抽象类的方式实现,生命具体对象,以及抽象方法: Con ...

  5. CentOS7 下安装telnet服务

    今天搞了下 Centos 7 下面升级 openssl 和 openssh ,顺便装了下 telnet # 安装 telnet 避免 ssh 无法登录 yum -y install xinetd te ...

  6. 杂谈3之English

    1.面向过程(OPP):Orient Procedure Program (C语言) 2.面向对象(OOP):Orient ObjectProgram(Java) 3.面向对象的三大特征:继承Inhe ...

  7. Ajax实现页面动态加载,添加数据

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Products ...

  8. 【锋利的Jquery】读书笔记三

    DOM操作 三个方面;DOM core    html-dom  css-dom 注意点: 删除事件中 三种删除节点的方法   remove   detach   empty remove不解释 de ...

  9. 洛谷-乘积最大-NOIP2000提高组复赛

    题目描述 Description 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你 ...

  10. C#基础1:Console类

    Console相关:   1.输出到控制台 Console.Write(输出的值);  表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入.Console.WriteLine(输出的值) ...