题目:

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.

链接: http://leetcode.com/problems/search-a-2d-matrix/

题解:

可以把矩阵当做一个长的行向量进行binary search, 也可以对行列分别进行两次binary search,都差不多。

Time Complexity - O(log(mn)),  Space Complexity - O(1).

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0)
return false;
int rowNum = matrix.length, colNum = matrix[0].length;
int lo = 0, hi = rowNum * colNum - 1; while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
int row = mid / colNum, col = mid % colNum;
if(matrix[row][col] < target)
lo = mid + 1;
else if(matrix[row][col] > target)
hi = mid - 1;
else
return true;
} return false;
}
}

二刷:

跟一刷一样,利用二分搜索, 然后在搜索的时候把mid转化为矩阵的行和列坐标就可以了。

Time Complexity - O(logmn),  Space Complexity - O(1).

Java:

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) {
return false;
}
int rowNum = matrix.length, colNum = matrix[0].length;
int lo = 0, hi = rowNum * colNum - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int row = mid / colNum;
int col = mid % colNum;
if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] < target) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return false;
}
}

三刷:

把2D Matrix转换为1D Array,然后使用Binary Search就可以了。假设1D Array中的index是mid,转换就是  row = mid / colNum, col = mid % colNum

Java:

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) { return false; }
int rowNum = matrix.length, colNum = matrix[0].length;
int lo = 0, hi = rowNum * colNum - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int rowMid = mid / colNum;
int colMid = mid % colNum;
if (matrix[rowMid][colMid] == target) { return true; }
else if (matrix[rowMid][colMid] < target) { lo = mid + 1; }
else { hi = mid - 1; }
}
return false;
}
}

测试:

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

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

随机推荐

  1. openerp学习笔记 计算字段、关联字段(7.0中非计算字段、关联字段只读时无法修改保存的问题暂未解决)

    计算字段.关联字段,对象修改时自动变更保存(当 store=True 时),当 store=False 时,默认不支持过滤和分组7.0中非计算字段.关联字段只读时无法修改保存的问题暂未解决 示例代码: ...

  2. OpenWRT 路由配置技巧

    随着最近 Google 在国内已经完全无法访问,使得通过 VPN 访问网络的需求更加强烈,本文介绍的方法可以使一个普通的路由具备稳定连接 VPN 的能力,并能够根据目标访问网站选择国内外线路,从而得到 ...

  3. Mininet VM设置笔记

    Mininet VM是为了加快Mininet安装,而且可以很容易在linux平台上运行. VM运行在Windows,Mac,Linux,通过VMware.VirtualBox,QEMU和KVM. 下载 ...

  4. 2016 系统设计第一期 (档案一)MVC 控制器接收表单数据

    1.FormCollection collection   user.UserId =Convert.ToInt32(collection["UserId"]); /// < ...

  5. NET免费服务器

    NET免费服务器 1.先注册一个号.地址:https://appharbor.com/ 2.看看有没有你需要的插件,基本上都是免费的 3.本地创建git库 4.复制git远程仓库的地址 5.推送到远程 ...

  6. Python 安装 httpie

    Python 安装 httpie 前段时间开发RESTful的程序,使用浏览器插件HttpRequester,挺高级,易用的.后来在RESTHeart项目中认识了httpie,感觉高大上.在使用htt ...

  7. Elasticsearch 5.0

    Elasticsearch 5.0 使用ES的基本都会使用过head,但是版本升级到5.0后,head插件就不好使了.下面就看看如何在5.0中启动Head插件吧! 官方粗略教程 Running wit ...

  8. C#基础原理拾遗——面试都爱问的委托和事件(纠正)

    这篇博客是我昨天写的,文中的观点有些问题,后经过网友留言和个人学习发现错误,原文还是保留,更改补在后面,不怕贻笑大方,唯恐误人子弟.不知道还能不能放在首页,让被误导的同学再被反误导一次. 一.原文 几 ...

  9. 模仿易信的UI

    易信,它的UI还是很简洁,因此本人想模仿一下它,用了一天的时候来研究它的资源文件,终于被我写出来.先看下效果图吧.     (一)首页的标题     main_title.xml <?xml v ...

  10. 详解HTML5中的<aside>元素与<article>元素

    <aside>元素HTML<aside>元素表示一个页面的一部分, 它的内容跟这个页面的其它内容的关联性不强,或者是没有关联,单独存在.<aside>元素通常显示成 ...