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 ...
随机推荐
- HDOJ 4085 Peach Blossom Spring
discriptionTao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous w ...
- [AGC025E]Walking on a Tree
题意:有一棵树,你要按顺序在树上走$m$次,每次从$u_i$到$v_i$或从$v_i$到$u_i$,走完后,如果一条边被单向经过,那么它贡献$1$的价值,如果一条边被双向经过,那么它贡献$2$的价值, ...
- Jackson使用ObjectManage#readValue传入泛型T的问题
说明:没找到合适的方法,持续关注这个问题 参考: https://stackoverflow.com/questions/11664894/jackson-deserialize-using-gene ...
- ionic 打包成apk后,所有网络请求404
无论怎么改 config.xml <allow-navigation href="http://*/*" /> <allow-intent href=" ...
- MythXinWCF通用宿主绿色版V1.2发布,及服务启动相关说明
最新下载地址 更新日志: 1.宿主配置增加了最大连接及队列数.允许数据传输量 2.程序大量优化 3.bug修正 4.增加已服务方式启动 点击服务方式启动后,会将软件注册为服务. 然后软件就会变成一 ...
- OpenCV 64位时 应用程序无法正常启动0x000007b 问题解决
这问题根本不是DirectX问题,不知道网上怎么这么这样的回复.而且也不亲自验证一下.下面将自己花很多时间才解决的方式整理一下. 因为一般情况下你配置的OpenCV加入系统环境变量的都是X86下的bi ...
- webpack-dev-server最简单的应用
1.安装 npm install webpack-dev-server --save-dev 2.再exports后加多一个对象即可 devServer: { contentBase: ". ...
- Protostuff序列化工具类
源代码 package org.wit.ff.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStre ...
- VS2010中使用命令行參数
在Linux下编程习惯了使用命令行參数,故使用VS2010时也尝试了一下. 新建项目,c++编敲代码例如以下: #include<iostream> #include<fstream ...
- RequireJS全面讲解
异步模块定义(AMD) 谈起RequireJS,你无法绕过提及JavaScript模块是什么,以及AMD是什么. JavaScript模块只是遵循SRP(Single Responsibility ...