28. Search a 2D Matrix 【easy】
28. Search a 2D Matrix 【easy】
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
.
O(log(n) + log(m)) time
解法一:
class Solution {
public:
/*
* @param matrix: matrix, a list of lists of integers
* @param target: An integer
* @return: a boolean, indicate whether matrix contains target
*/
bool searchMatrix(vector<vector<int>> &matrix, int target) {
int row = matrix.size();
if (row == ) {
return false;
} int col = matrix[].size();
if (col == ) {
return false;
} int start = ;
int end = row * col - ; while (start + < end) {
int mid = start + (end - start) / ; if (matrix[mid / col][mid % col] == target) {
return true;
}
else if (matrix[mid / col][mid % col] < target) {
start = mid;
}
else if (matrix[mid / col][mid % col] > target) {
end = mid;
}
} if (matrix[start / col][start % col] == target
|| matrix[end / col][end % col] == target) {
return true;
}
else {
return false;
}
}
};
注意:
1、边界值合法性检查
2、二维数组转一维数组的计算方式
解法二:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == ) {
return false;
}
if (matrix[] == null || matrix[].length == ) {
return false;
} int row = matrix.length;
int column = matrix[].length; // find the row index, the last number <= target
int start = , end = row - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (matrix[mid][] == target) {
return true;
} else if (matrix[mid][] < target) {
start = mid;
} else {
end = mid;
}
}
if (matrix[end][] <= target) {
row = end;
} else if (matrix[start][] <= target) {
row = start;
} else {
return false;
} // find the column index, the number equal to target
start = ;
end = column - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (matrix[row][mid] == target) {
return true;
} else if (matrix[row][mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (matrix[row][start] == target) {
return true;
} else if (matrix[row][end] == target) {
return true;
}
return false;
}
}
两次二分,值得借鉴!
28. Search a 2D Matrix 【easy】的更多相关文章
- Search a 2D Matrix【python】
class Solution: # @param matrix, a list of lists of integers # @param target, an integer # @return a ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 【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】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【刷题-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 ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
随机推荐
- (转)[Unity3D]关于Assets资源目录结构管理
分享个我们项目常用的目录结构,微调过很多次,最终到了这个版本.个人认为这种管理资源方式是不错的.欢迎探讨各个细节~ 更新于2013.5.30 Asserts --Editor 自写的灵活方便插 ...
- activemq 5.13.2 jdbc 数据库持久化 异常 找不到驱动程序
原文:https://my.oschina.net/u/2284972/blog/662033 摘要: activemq jdbc 数据库持久化 异常 找不到驱动程序 Caused by: java. ...
- 【spring data jpa】 spring data jpa 中 时间格式设置between and 查询
实例代码: //举报时间 Date createDate = entity.getCreateDate(); if (createDate != null){ predicates.add(cb.be ...
- Error:Execution failed for task ‘:app:processDebugManifest’.
Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed : Attribute ...
- 关于yum的一些安装问题
最近折腾CentOS和kubernetes,遇到一些安装问题,把和yum相关的逐步总结如下: 如何用本地的cdrom作为yum源 mount /dev/cdrom /mnt 先查询是否安装了creat ...
- Hive使用简介
---恢复内容开始--- 指定分隔符 HIVE输出到文件的分隔符 ,列与列之间是'\1'(ASCII码1,在vim里显示为^A),列内部随着层数增加,分隔符依次为'\2','\3','\4'等. 例: ...
- GPU Skin
http://blog.csdn.net/leonwei/article/details/77387357 TPOSE存vbo 每根骨骼的matrices存在貼圖裏用vertex fetch 做GPU ...
- Maven 使用 二——nexus
上篇博客介绍了创建maven项目的两种方式,当中一种是使用命令行的方式来创建,这种情况非常少,一般我们都有IDE开发环境.所以接下来,我们还是在一个详细的IDE中来说,我使用的是Eclipse. 一. ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- 深入解析淘宝Diamond之客户端架构
转载:http://blog.csdn.net/u013970991/article/details/52088350 一.什么是Diamond diamond是淘宝内部使用的一个管理持久配置的系统, ...