Search a 2D Matrix ——LeetCode
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 from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3
, return true
.
题目大意:给一个m*n矩阵,每一行都是递增有序,逐行也是递增的,要求设计一个高效算法检查目标元素是否存在于此矩阵中。
解题思路:可以把整个矩阵展开,就是一个长数组,数组长度为M*N,用二分查找即可,就是需要把二分查找的位置转化为矩阵下标。假设有row行,col列,那么key对应的矩阵中的元素应该是matrix[key/col][key%col],这样就转为二分查找了。
Talk is cheap>>
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix[0][0] > target)
return false;
int rowLen = matrix.length;
int colLen = matrix[0].length;
int low = 0, high = rowLen * colLen - 1; while (low <= high) {
int mid = (low + high) >>> 1;
int x = mid / colLen;
int y = mid % colLen;
if (matrix[x][y] > target) {
high = mid - 1;
} else if (matrix[x][y] < target) {
low = mid + 1;
} else {
return true;
}
}
return false;
}
Search a 2D Matrix ——LeetCode的更多相关文章
- Search a 2D Matrix leetcode java
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- Search a 2D Matrix leetcode
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- GCOV 使用用例
1.GCOV查看arm-linux代码覆盖率 一. 关于gcov工具 gcov伴随gcc 发布.gcc编译加入-fprofile-arcs -ftest-coverage 参数 ...
- Android(java)学习笔记234: 服务(service)之音乐播放器
1.我们播放音乐,希望在后台长期运行,不希望因为内存不足等等原因,从而导致被gc回收,音乐播放终止,所以我们这里使用服务Service创建一个音乐播放器. 2.创建一个音乐播放器项目(使用服务) (1 ...
- php laravel mysql无法连接处理方案(linux服务器配置)
阿里云 Ubuntu 14.*上搭建laravel环境 之前做项目时都是搭建在自己的服务器上,可是自己的那个服务器是很久以前一点点配置好的,也是各种百度,该忘记的都忘了, 所以前一段在客户的阿里云Ub ...
- codevs2059逃出克隆岛(传送门bfs)
/* 和普通的迷宫问题类似只是多了一个叫传送门的东西 对于传送门的处理: 每当跑到传送门就把其余所有传送门周围的点都入队 传送门之间不花费时间并且从不是传送门的点走到传送门 也不花费时间花费时间的(好 ...
- codevs4189字典(字典树)
/* 本字典树较弱 只支持插入单词 查询单词. 特殊的 bool变量w 标记此字母是不是某个单词的结束 (然而这个题并没卵用) */ #include<iostream> #include ...
- VS2008 快捷键大全--------<<转>>
VS2008 快捷键大全 Ctrl+E,D ---- 格式化全部代码 Ctrl+K,F ---- 格式化选中的代码 CTRL + SHIFT + B ...
- AIDL跨进程通信
Android跨进程通信会用到AIDL,当然跨进程通信不一定要用AIDL,像广播也是可以的,当然这里用到AIDL相对比较安全一些: AIDL允许传递基本数据类型(Java 的原生类型如int/long ...
- 创建DBLink语句
--linkName DBLink名 --username 用户名 --password 密码 --tns TNS配置字符串 create database link &linkName co ...
- 对DNSPOD添加域名解析的一些见解
1.主机记录这步比较简单,输入“www”表示比较常规的域名例如www.abc.com,“@”表示abc.com,“ * ”表示泛解析,匹配所有*.abc.com的域名. 2.记录类型这步,一般常用A记 ...
- iOS中常用的四种数据持久化技术
iOS中的数据持久化方式,基本上有以下四种:属性列表 对象归档 SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults st ...