Leetcode 074 Search a 2D Matrix
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.
思路:先利用每一行的最后一个数与target判断,确定target可能在的行数;再在确定的那一行进行折半查找。
public class S074 {
public boolean searchMatrix(int[][] matrix, int target) {
int i = 0;
for (;i<matrix.length;i++) {
if (matrix[i][matrix[0].length-1]>=target) {
break;
}
}
if (i == matrix.length)
return false;
int l = 0, r = matrix[0].length-1;
//折半查找
while (l <= r) {
if (target == matrix[i][(l+r)/2])
return true;
else if (target > matrix[i][(l+r)/2])
l = (l+r)/2+1;
else
r = (l+r)/2-1;
}
return false;
}
}
Leetcode 074 Search a 2D Matrix的更多相关文章
- Java for LeetCode 074 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] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [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] 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 ...
- 【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 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 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 ...
- [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 ...
随机推荐
- android studio 程序错误
一.错误类型: com.android.tools.fd.runtime.BootstrapApplication cannot be cast to 成功修改方式 File --> Setti ...
- ZentaoPHP
1.框架文档http://devel.cnezsoft.com/book/zentaophphelp/about-10.html zentaoPHP二次开发文档http://devel.cnezsof ...
- Jquery 2个数组,去除重复的项目
长数组(Positions_Check) 短数组(PositionTitlesParent_Check) for (var i = 0; i < PositionTitlesParent_Che ...
- APP模板框架
HTML页面 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...
- PL SQL Developer报错框乱码
在系统变量里设置 变量名:NLS_LANG 变量值设为:SIMPLIFIED CHINESE_CHINA.ZHS16GBK
- 调皮的R文件,卑鄙的空格
毕业快一年了,由于公司业务需要,开发岗的我做了一年测试.最近,终于要开始转开发了.于是和小伙伴们合作,做一个备忘录apk.由于之前是做java的,而且差不多一年没碰代码了(这一年主要做测试,虽然有写自 ...
- 拓展自定义编辑器窗口(EditorGUILayout类)
Unity支持自行创建窗口,也支持自定义窗口布局.在Project视图中创建一个Editor文件夹,在文件夹中再创建一条脚本. 自定义窗口需要让脚本继承EditorWindow再设置MenuItem, ...
- C#代码篇:代码产生一个csv文件调用有两个核心的坑
忙活了半天终于可以开工了,a物品到底要不要放进去取决于两个因素,第一是a有4kg重,只有背包大于等于4kg的时候才能装进去(也就是说当i=1,k<4时f[i,k]=0):第二是当背包的重量大于等 ...
- ng-Directive
伪代码: var myModule = angular.module(...); myModule.directive('namespaceDirectiveName', function facto ...
- 使用vs2010打开VS2013的工程文件
在开发团队,会出现vs工具使用版本的不一样的情况.我的电脑使用的是VS2010,可是其他人员使用的是vs2013. 要打开其他人员上传的工程文件,可以通过三种方式: 1.下载一个vs2013版本. 2 ...