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 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
.
解题思路:
二分查找即可,JAVA实现如下:
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0)
return false;
int up = 0, down = matrix.length-1, left = 0, right = matrix[0].length-1;
while (up < down) {
if(target>matrix[(up+down)/2][0]){
up=(up+down)/2;
if(target>matrix[up][matrix[0].length-1])
up++;
else break;
}
else if(target<matrix[(up+down)/2][0])
down=(up+down)/2-1;
else return true;
}
while(left<right){
if(target>matrix[up][(left+right)/2])
left=(left+right)/2+1;
else if(target<matrix[up][(left+right)/2])
right=(left+right)/2-1;
else return true;
}
return target==matrix[up][left];
}
当然,也可以只用两个指针表示,JAVA实现如下:
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0)
return false;
int l = 0, r = matrix.length * matrix[0].length - 1, mid = 0;
while (l < r) {
mid = l + (r - l) / 2;
if (matrix[mid/matrix[0].length][mid%matrix[0].length] < target)
l = mid + 1;
else if (matrix[mid/matrix[0].length][mid%matrix[0].length] > target)
r = mid;
else return true;
}
return matrix[l/matrix[0].length][l%matrix[0].length] == target;
}
Java for LeetCode 074 Search a 2D Matrix的更多相关文章
- 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 ...
- 074 Search a 2D Matrix 搜索二维矩阵
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[ [1, 3, ...
随机推荐
- 【SGU 104】Little shop of flowers
题意 每个花按序号顺序放到窗口,不同窗口可有不同观赏值,所有花都要放上去,求最大观赏值和花的位置. 分析 dp,dp[i][j]表示前i朵花最后一朵在j位置的最大总观赏值. dp[i][j]=max( ...
- bzoj3555 企鹅QQ
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1640 Solved: 613 Description P ...
- HDU 2896 病毒侵袭
Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福 ...
- make xconfig时,出现“Unable to find any QT installation"错误
在make menuconfig的时候,提示是找不到libncurses,最后其实是找不到libncurses-dev,又因为debian系统装的是libncurses5,所以装了libncurses ...
- form的submit与onsubmit的用法与区别
发生顺序:onsubmit -> submit1.阻止表单提单:<script>function submitFun(){ //逻辑判断 return true; //允 ...
- iOS-Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its super
今天在XCode6.3上面重写TabBar的时候,自定义tabBar的代理遇到的一个问题 在重写tabBar的代理的时候遇到了一个警告. 解决方法: 在.m文件中 警告消失.
- [实战]MVC5+EF6+MySql企业网盘实战(27)——应用列表
写在前面 本篇文章将实现应用列表,同样和其他列表的不同之处,在于查询条件的不同. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘 ...
- 关于31天App教程示例中一些因SDK版本而出现的问题(转)
由于国外那个知名的31天案例教程比较老,所用官方API是2008年时的2.X,所以在现在的Xcode3-4之后或多或少都有编译警告和错误信息.必须做些适应iOS版本的代码更改才能顺利编译通过. Day ...
- 还是畅通工程(MST)
还是畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- Swift Tour 随笔总结 (3)
关于Optional的Control Flow if let constantName = someOptional { statements } 如果该Optional为nil,则不进入if,否则执 ...