搜索二维矩阵
来源:互联网 发布:网页游戏自动挂机软件 编辑:程序博客网 时间:2024/05/16 13:01
写出一个高效的算法来搜索 m × n矩阵中的值。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每行的第一个数大于上一行的最后一个整数。
考虑下列矩阵:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
给出
target = 3
,返回true
public class Solution {
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
int row = matrix.length;
if(row==0)
return false;
int col = matrix[0].length-1;
for(int i = 0 ; i < row; i++){
if(matrix[i][col] == target)
return true;
if(matrix[i][col] > target){
for(int j = 0;j<col;j++){
if(matrix[i][j] == target)
return true;
if(matrix[i][j]<target && matrix[i][j+1]>target)
return false;
}
}
}
return false;//针对数组只有一个数且不是target的情况
}
}
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
int row = matrix.length;
if(row==0)
return false;
int col = matrix[0].length-1;
for(int i = 0 ; i < row; i++){
if(matrix[i][col] == target)
return true;
if(matrix[i][col] > target){
for(int j = 0;j<col;j++){
if(matrix[i][j] == target)
return true;
if(matrix[i][j]<target && matrix[i][j+1]>target)
return false;
}
}
}
return false;//针对数组只有一个数且不是target的情况
}
}
挑战 思路:可转化为排序好的一维数组,然后通过二分法查找,matrix[i][j]在一维数组的第i*m+j个位置
O(log(n) + log(m)) 时间复杂度
public class Solution {
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
int row = matrix.length;
if(row==0)
return false;
int col = matrix[0].length;
int low = 0;
int high = row*col-1;
while(low + 1 < high){//最后low和high挨着的时候就跳出循环
int mid = (low + high)/2;
int r = mid / col;//行坐标
int c = mid % col;//列坐标
if(matrix[r][c] == target)
return true;
else if(matrix[r][c] < target)
low = mid;
else
high = mid;
}
//循环结束 判断一下最后相邻的low和high
if(matrix[low/col][low%col]==target)
return true;
if(matrix[high/col][high%col]==target)
return true;
return false;
}
}
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
int row = matrix.length;
if(row==0)
return false;
int col = matrix[0].length;
int low = 0;
int high = row*col-1;
while(low + 1 < high){//最后low和high挨着的时候就跳出循环
int mid = (low + high)/2;
int r = mid / col;//行坐标
int c = mid % col;//列坐标
if(matrix[r][c] == target)
return true;
else if(matrix[r][c] < target)
low = mid;
else
high = mid;
}
//循环结束 判断一下最后相邻的low和high
if(matrix[low/col][low%col]==target)
return true;
if(matrix[high/col][high%col]==target)
return true;
return false;
}
}
0 0
- 题目:搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵 II
- 搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 搜索二维矩阵II
- 【LintCode】搜索二维矩阵
- 搜索二维矩阵
- lintcode,搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵 II
- LintCode : 搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵
- 【JavaScript】 判断横竖屏
- C++开源日志库选择
- java 稀疏矩阵
- 解决LibclangError: [Error 193] %1 不是有效的 Win32. To provide e Config.set_library_path() or Config.
- 一:Spark介绍
- 搜索二维矩阵
- 头文件和宏模板
- mybatis一对多查询过程只查询了一个结果
- FFMpeg Intro - Subtitles and Metadata
- 简介Swift面向协议编程
- 设置Sublime Text新标签页打开文件
- 【JavaScript】表单效验
- mysql创建外键原则
- 使用SimpleDateFormat需要注意的地方