LeetCode-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.
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
boolean found=false;
if(matrix==null){
return found;
}
int m=matrix.length;
int n=matrix[0].length; int upRow=0;
int downRow=m-1; while(upRow<=downRow){
if(target<=matrix[upRow][n-1]){
found=binarySearch(matrix, upRow, target);
break;
}
else if(target>=matrix[downRow][0]){
found=binarySearch(matrix, downRow, target);
break;
}
else{
upRow++;
downRow--;
}
}
return found;
} public boolean binarySearch(int[][]matrix, int row, int target){
int m=matrix.length;
int n=matrix[0].length; int left=0;
int right=n-1; boolean found=false;
while(left<=right){
int mid=left+(right-left)/2;
if(matrix[row][mid]==target){
found=true;
break;
}
else if(matrix[row][mid]<target){
left=mid+1;
}
else{
right=mid-1;
}
}
return found;
}
}
LeetCode-Search a 2D Matrix的更多相关文章
- [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: 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 -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- 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 @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [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 二分搜索
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 (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
随机推荐
- C#中DateTime.Now.ToString()
项目开发中遇到一个问题:C#编写的SQL语句中有时间值,刚开始直接将DateTime.Now进行toString()处理,源代码调试程序运行正常. 然后我的电脑重装了系统,再次运行程序就报错“从字符串 ...
- run方法和start方法的不同
run 方法只不过是对对象方法的简单调用,在主线程中的执行时间是固定的 而start方法是开启一个线程,起执行时间是不固定的.
- Apache Tomcat开机后台启动
作为软件开发人员,经常接触Tomcat,完成的项目,需要部署到服务器上的Tomcat,才能供其他人访问浏览. 因为存在以下问题,所以需要把Tomcat设置为后台自动启动: 1.服务器可能因环境故障面临 ...
- swift 构建类
参开 http://blog.csdn.net/chelongfei/article/details/49784633 在 Swift 中, 类的初始化有两种方式, 分别是 Designated In ...
- 使用crosswalk优化ionic2应用包
ionic plugin add cordova-plugin-crosswalk-webview --save
- 2016 - 2 - 19 ARC内存管理知识总结(一,arc基本概念及alloc等方法的实现)
一. ARC的基本概念 1. 在objc中采用automatic reference counting 机制, 让编译器来进行内存管理.在降低程序崩溃,内存管理泄漏等风险的同时,很大程度减少了程序员的 ...
- Unity Ugui射线坐标转换总结
世界空间中的点坐标转换到屏幕坐标: screenPos = RectTransformUtility.WorldToScreenPoint(cam, worldPos.transform.positi ...
- Ngui中Sprite,SlicedSprite,Tiled Sprite,FilledSprite的区别
Sprite:标准Sprite控件,自适应图片大小. Sliced Sprite:一个含有9个切片的Sprite,创建固定边框的控件最佳选择,固定大小,不会随图片大小而改变,可做人物头像等. Tile ...
- python 字符串内建函数
方法 描述 string.capitalize() 把字符串的第一个字符大写 string.center(width) 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串 string ...
- POJ1385 计算多边形的重心
point gravity_center(point* p,int n) { double area=0.0; point ZERO; ZERO.x = 0; ZERO.y = ...