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 ...
随机推荐
- 【动态规划】【记忆化搜索】CODEVS 1011 数的计算 2001年NOIP全国联赛普及组
设答案为f(n),我们显然可以暴力地递归求解: f(n)=f(1)+f(2)+……+f(n/2). 但是n=1000,显然会超时. 考虑状态最多可能会有n种,经过大量的重复计算,所以可以记忆下来,减少 ...
- u-boot-2012.10移植到AT91RM9200(包括NAND FLASH)
基于中嵌SRM9204 目 录 1 配置 1.1修改顶层Makefile(可选) 1.2配置 1.3下载.运行.测试 2 修改内存配置参数(根据芯片手册修改) 2.1 修改配置参数 2.2 编译 2 ...
- mongodb_性能监控
一.使用mongostat.exe cd C:\Program Files\MongoDB\Server\3.0\bin\ --> mongostat.exe --> mongostat ...
- HTML5 canvas图形库 RGraph【转】
RGraph是一个使用HTML5 Canvas标签实现的图表制作Library.利用该Library生成的Chart具有可交互性,当鼠标点击或移过时会显示相应的信息,可以动态加载Chart或对特殊点进 ...
- Java计算两个日期相差的天数
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- Hibernate参数设置一览表
属性名 用途 hibernate.dialect 一个Hibernate Dialect类名允许Hibernate针对特定的关系数据库生成优化的SQL. 取值 full.classname.of.Di ...
- 怎样编写高效android代码
基于Android相关设备作为嵌入式设备范畴,在书写App应用的时候要格外关注效率.而且受电池电量的限制.这就导致嵌入式设备有诸多考虑.有限处理能力.因此就要求我们尽量去写高效的代码. 本文讨论了非常 ...
- Rails时间扩展方法
1.时间扩展方法: beginning_of_day,end_of_day等等 Time.now.end_of_day 2.ruby类型判断 is_a?(类型) 如:b.is_a?(Array)
- 微信小程序 - 上传图片纯前端(多张、单张)
演示如下 可能有些命名不太规范,到时改一下即可 点击从github拉取:图片上传示例
- [转]2年SQL Server DBA调优方面总结
2年SQL Server DBA调优方面总结 当2年dba 我觉得,有些东西需要和大家分享探讨,先书单. 书单 1.<深入解析SQL Server 2008 系列> 这个就是mssql ...