240. Search a 2D Matrix II
来源:互联网 发布:安卓手机变电脑软件 编辑:程序博客网 时间:2024/06/05 08:08
问题描述:
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 in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5
, return true
.
Given target = 20
, return false
.
解题思路:
思路是简单地对矩阵进行过滤压缩,首先,通过查找第一行和第一列,确定行与列的范围。然后对于每行进行二分查找。当然先根据有效的最后一列判断是否在对应范围内,如果不在,continue,继续进行查找,直到找到该元素。代码如下:
typedef int bool ;
#define true 1
#define false 0
bool binarySearch(int *row,int start,int end,int target)
{
int mid;
while(start<end){
mid=(start+end)/2;
if(row[mid]==target)
{
return true;
}
else if(row[mid]>target)
{
end=mid;
}
else {
start=mid+1;
}
}
return false;
}
int row,col;
if(matrix[0][col]>target)
break;
else if(matrix[0][col]==target)
return true;
}
int i,j;
for(i=1;i<row;i++){
if(matrix[i][col-1]<target)
continue;
else if(matrix[i][col-1]==target)
return true;
if(binarySearch(matrix[i],1,col,target))
return true;
}
return false;
{
int i,j;
int a[5][5]={{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
printf("true!");
问题描述:
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 in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5
, return true
.
Given target = 20
, return false
.
解题思路:
思路是简单地对矩阵进行过滤压缩,首先,通过查找第一行和第一列,确定行与列的范围。然后对于每行进行二分查找。当然先根据有效的最后一列判断是否在对应范围内,如果不在,continue,继续进行查找,直到找到该元素。代码如下:
typedef int bool ;
#define true 1
#define false 0
bool binarySearch(int *row,int start,int end,int target)
{
int mid;
while(start<end){
mid=(start+end)/2;
if(row[mid]==target)
{
return true;
}
else if(row[mid]>target)
{
end=mid;
}
else {
start=mid+1;
}
}
return false;
}
int row,col;
if(matrix[0][col]>target)
break;
else if(matrix[0][col]==target)
return true;
}
int i,j;
for(i=1;i<row;i++){
if(matrix[i][col-1]<target)
continue;
else if(matrix[i][col-1]==target)
return true;
if(binarySearch(matrix[i],1,col,target))
return true;
}
return false;
{
int i,j;
int a[5][5]={{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
printf("true!");
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240.Search a 2D Matrix II
- PREROUTING 和 POSTROUTING, SNAT 和 DNAT图文解析(非常清淅)
- iOS多线程总结
- python基础笔记-8
- centos系统安装mongdb
- 常见模块设计--权限管理(一)
- 240. Search a 2D Matrix II
- python学习之指定脚本解释器
- 批处理实现畸形文件、文件夹创建、访问、重命名、删除
- 工作随感三小点
- C++之详解coredump
- InnoDB索引实现Myisam区别
- 文章标题
- mybatis中的#号和$号的区别
- uboot执行顺序main_loop