题目:

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. KBase使用教程

    1. SELECT * FROM EXPERT_BASEINFO WHERE (源照片='*' not 源照片 is null) and 标准一级机构='山东大学' and 当前职称='*教授' 2. ...

  2. pyQuery的安装

    1. 直接通过pip安装 你会发现lxml怎么搞都报错,后来单独先安装libxml2和libxslt pip是找不到这个包的,只好百度.发现有很多的例子的解决方案.后来发现了个实用的. 2. 先安装l ...

  3. LINQ.CS

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Zdso ...

  4. Mysql ubuntu下的安装卸载

    删除 mysql sudo apt-get autoremove --purge mysql-server-5.0sudo apt-get remove mysql-serversudo apt-ge ...

  5. 【ajax跨域】原因原理解决

    1.安全,跨域cookie iframe 2.很简单,就是利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的.当需要通讯时,本站脚本创建一个<scr ...

  6. JAVA类与对象(三)----类定义关键字详解

    static 表示静态,它可以修饰属性,方法和代码块. 1.static修饰属性(类变量),那么这个属性就可以用类名.属性名来访问,也就是使这个属性成为本类的类变量,为本类对象所共有.这个属性就是全类 ...

  7. php获取指定日期所在星期的开始时间与结束时间

    function getWeekRange($date){    $ret=array();    $timestamp=strtotime($date);    $w=strftime('%u',$ ...

  8. 【学习总结】iOS 数据保存几种方式总结

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: NSKeyedAr ...

  9. 微软职位内部推荐-Senior Android Developer

    微软近期Open的职位: Position: Senior SDE-- Mobile Products Android/iOS/WP Senior Developer Contact Person: ...

  10. Daily Scrum 11.11

    摘要:本次会议继续讨论程序的问题以及单元测试和集成测试,本次测试为1.02版本.本次的Task列表如下: Task列表 出席人员 Today's Task Tomorrow's Task 刘昊岩  t ...