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 ...
随机推荐
- Repeat Header / Keep Header Visible in Tables in RS 2008
You selected "Repeat header rows on each page" or "Keep header rows visible while scr ...
- iOS应用的真机调试
必须条件:99美元的帐号,没有这个就不用再往下看了. 首先,登录到http://developer.apple.com/devcenter/ios/index.action,如果已经购买了iPhone ...
- _cpluscplus
_cpluscplus是c++中的定义,而c中没有该定义 1.用来判定代码是c类型还是c++类型 2._cplusplus的类型是"long int",值为199711L int ...
- MVC 使用 FluentScheduler 定时器计划任务
MVC 使用 FluentScheduler 定时器计划任务 MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲 ...
- [algothrim]URL相似度计算的思考
http://www.spongeliu.com/399.html http://in.sdo.com/?p=865
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能
MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能 由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQ ...
- 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。 n<=39
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- 【Ural】【1057】Amount of degrees
数位DP 2009年刘聪<浅谈数位类统计问题> 例题一 从组合数 以及 数位DP的角度都可以做…… 首先转化成求1~n内K进制下只有0.1的数的个数: 考虑K进制下第一个为1的位,剩下的数 ...
- WinForm员工信息表
先搞一个panel,然后里面放label.
- 中国餐馆过程(CRP)
查如何事先确定聚类簇数目发现的,是对狄利克雷过程的(DP)的一种解释. 假设一个中国餐馆有无限的桌子,第一个顾客到来之后坐在第一张桌子上.第二个顾客来到可以选择坐在第一张桌子上,也可以选择坐在一张新的 ...