Search a 2D Matrix | & II
Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.
This matrix has the following properties:
- Integers in each row are sorted from left to right.
- Integers in each column are sorted from up to bottom.
- No duplicate integers in each row or column.
Consider the following matrix:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
Given target = 3
, return 2
.
分析:
因为数组里的数有上面三个特性,所以我们可以从左下角开始找。如果当前值比target大,明显往上走,比target小,往右走,如果一样斜上走。Time complexity: (O(m + n)) (m = matrix.length, n = matrix[0].length)
public class Solution {
public int searchMatrix(int[][] matrix, int target) {
// check corner case
if (matrix == null || matrix.length == 0 || matrix[].length == 0) {
return ;
}
// from bottom left to top right
int x = matrix.length - ;
int y = ;
int count = ; while (x >= && y < matrix[].length) {
if (matrix[x][y] < target) {
y++;
} else if (matrix[x][y] > target) {
x--;
} else {
count++;
x--;
y++;
}
}
return count; }
}
Search a 2D Matrix I
Write an efficient algorithm that searches for a value in an mx 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.
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3
, return true
.
分析:
根据数组的特点,我们需要找出一个大范围(row),然后再确定小范围。
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == || matrix[].length == ) return false;
int rowCount = matrix.length, colCount = matrix[].length;
if (matrix[][] > target || matrix[rowCount - ][colCount - ] < target) return false; int row = getRowNumber(matrix, target);
int column = getColumnNumber(matrix, row, target); return matrix[row][column] == target; } public int getRowNumber(int[][] matrix, int target) {
int start = , end = matrix.length - , width = matrix[].length;
while (start <= end) {
int mid = start + (end - start) / ;
if (matrix[mid][width - ] == target) {
return mid;
} else if (matrix[mid][width - ] > target) {
end = mid - ;
} else {
start = mid + ;
}
}
return start;
} public int getColumnNumber(int[][] matrix, int row, int target) {
int start = , end = matrix[].length; while (start <= end) {
int mid = start + (end - start) / ;
if (matrix[row][mid] == target) {
return mid;
} else if (matrix[row][mid] > target) {
end = mid - ;
} else {
start = mid + ;
}
}
return start;
}
}
Search a 2D Matrix | & II的更多相关文章
- LintCode 38. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- 【刷题-LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
随机推荐
- 5.9-4用字符串生成器给字符串str追加1~10这10个数字
package zfc; public class ZfcShcq { public static void main(String[] args) { // TODO Auto-generated ...
- MVC学习Day01
~~~~ =============================================================================================== ...
- AC自动机(转)
http://www.cppblog.com/mythit/archive/2009/04/21/80633.html 首先简要介绍一下AC自动机:Aho-Corasick automation,该算 ...
- PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法
逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...
- POJ3070Fibonacci(矩阵快速幂+高效)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11587 Accepted: 8229 Descri ...
- Activator.CreateInstance 反射实例化对象
public class CommonReq { private String TransNo { get; set;} public String SubmitData { get; set; } ...
- std::bind(二)
bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板. 用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看 ...
- 锋利的jQuery-7--一个$.fn.color插件的编写过程
编写一个设置和获取元素的color的插件: 首先实现第一个功能,设置: ;(function($){ $.fn.extend({ color:function(value){ return this. ...
- ASP.NET MVC4中使用Ninject
1.NuGet获取Ninject.dll 选中项目右键: .NET技术交流群 199281001 .欢迎加入. 2.全局注册 Global.asax.cs //注册Ninject依赖注入全局解析器 ...
- derby数据库ql语法
[数据库知识] 主键.唯一键包含索引 主键包含唯一键.索引.非空 唯一键包含索引,可空或非空 数据库需要与执行服务的在同个目录下 唯一键 create table app.tyu ( primaryk ...