74. Search a 2D Matrix
题目:
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的更多相关文章
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [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 ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- How to add EDT relation table[AX2012]
setp 1. First create New Edit. setp 2.Create New Table First Table Name is NParentRel then drag and ...
- LAMP开发之环境搭建(2014.12.7在ubuntu下)
Ubuntu下搭建LAMP环境 前言:学习PHP脚本编程语言之前,必须先搭建并熟悉开发环境,开发环境有很多种,例如LAMP.WAMP.MAMP等.这里我搭建的是LAMP环境,即Linux.Apache ...
- The difference between Union & Union All in SQL Server/pOSTGRESQL
Following is test in SQL Server: USE [TestDB] CREATE TABLE [dbo].[UserInfoTest02]( [number] [bigint] ...
- OC的类方法、对象方法和函数
OC语言中的方法和函数是有区别的:类内部叫方法,单独定义的叫函数,定义的格式也不同 类方法:+ (void) 方法名.对象方法:- (void) 方法名.函数:void 函数名(参数列表) #impo ...
- 编译时和运行时、OC中对象的动态编译机制
编译时 编译时顾名思义就是正在编译的时候.那啥叫编译呢?就是编译器帮你把源代码翻译成机器能识别的代码.(当然只是一般意义上这么说,实际上可能只是翻译成某个中间状态的语言.比如Java只有JVM识别的字 ...
- linux驱动之I2C
include/linux/i2c.h struct i2c_msg;struct i2c_algorithm;struct i2c_adapter;struct i2c_client;struct ...
- Ui设计哪里有好的素材
刚看到花瓣网,的确不错,以后得多逛逛了.(不喷广告,只留作笔记)
- lamada 表达式之神奇的groupby
少说话多干活 先定义一个测试用的实体,接下来会用字段Name进行分组的 public class TestToRun { public string Name { get; set; }//名称 pu ...
- AsyncTask不能同时运行多个实例解决办法
在项目中使用AsyncTask时,发现创建的多个实例无法同时运行,比如: AsyncTask t1 = new MyTask(); AsyncTask t2 = new MyTask(); t1.ex ...
- oracle 11g行转列 列转行
行转列: SELECT * FROM src_table UNPIVOT (param_value FOR param_name IN (product_color AS 'product ...