题目:

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. Elastix 禁用SSL(https),还原为 http 访问

    1.相关配置文件目录: Apache的配置文件:httpd.conf,位于:/etc/httpd/conf/httpd.conf,配置文件中包含 Include conf.d/*.conf ,引入了  ...

  2. JNI 学习笔记

    JNI是Java Native Interface的缩写,JNI是一种机制,有了它就可以在java程序中调用其他native代码,或者使native代码调用java层的代码.也 就是说,有了JNI我们 ...

  3. 9、XAML名称空间详解

    XAML命名空间 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      ...

  4. Converting Storyboard from iPhone to iPad

    I found out a kind of solution: Duplicate your iPhone-Storyboard and rename it MainStoryboard_iPad.s ...

  5. Oracle 存储过程实例

    create or replace procedure PCREPORT is startDate DATE; --起始如期 nowTime DATE; --当前日期 nowTime2 DATE; - ...

  6. 关于 iOS10 更新后 360 云盘 的上传按钮消失的解决方案

    最近出了iOS10,作为iOS开发者,果断更新. 但是更新完后,打开自己的360云盘,发现想向云盘上传东西,但是传不了,加号按钮不见了. 经过我的研究,原因是 下面的自定义tabbar放置加号按钮的方 ...

  7. Regex.Match 方法

    Regex.Match 方法 在输入字符串中搜索正则表达式的匹配项,并将精确结果作为单个 Match 对象返回. 重载列表      (1) 在指定的输入字符串中搜索 Regex 构造函数中指定的正则 ...

  8. Python标准库之os模块

    1.删除和重命名文件 import os import string def replace(file, search_for, replace_with): # replace strings in ...

  9. tomcat内存溢出,设置

    tomcat/bin/catalina.bat里找到echo Using CATALINA_BASE:   "%CATALINA_BASE%" ,在上方设置:    set JAV ...

  10. BZOJ3473: 字符串

    3473: 字符串 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 109  Solved: 47[Submit][Status] Descriptio ...