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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [leetcode]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

  8. [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 ...

  9. [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 ...

  10. LeetCode Search a 2D Matrix II (技巧)

    题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...

随机推荐

  1. 012 VS2013常用快捷键

    2016-01-28 1.回到上一个光标位置/前进到下一个光标位置  1)回到上一个光标位置:使用组合键“Ctrl + -”: 2)前进到下一个光标位置:“Ctrl + Shift + - ”. 2. ...

  2. angularjs ng-click

    在angularjs的controller中一段代码,展示如下: var sortList = new SortList(); sortList.setSorts([$scope.year_inves ...

  3. java中的foreach循环

    foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简化版本,但是foreach语句并不能完全取代for语 ...

  4. Multiple View Geometry in Computer vision 1.1节部分翻译

    1.1简介—无处不在的投影几何 我们都熟悉射影变换.当我们看一幅图,我们看到的方形不是方形,或圆形不是圆形.平面立体映射到图片上的变换是一个投影变换的例子. 因此投影变换时保留的几何属性是什么呢?当然 ...

  5. 使用javaScript实现简单倒计时功能

    效果如下: <div class="warp"> <p id="txt">距离”十一“国庆放假还有:</p><br&g ...

  6. AJAX JSONP源码实现(原理解析)

    关于JSONP以及跨域问题,请自行搜索. 本文重点给出AJAX JSONP的模拟实现代码,代码中JSONP的基本原理也一目了然. <html xmlns="http://www.w3. ...

  7. 微信Auth2.0授权的时候出现两次回调

    在获取用户OpenID的时候 $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".WX_APPID. ...

  8. selenium配置

    1.firebug安装--火狐插件 2.firepath安装--火狐插件 3.

  9. 【CMD】

    1.dir 2. set (不带参数) 查看环境变量. SET [variable=[string]] variable  指定环境变量名. string    指定要指派给变量的一系列字符串. 3.

  10. jQuery原生框架中的jQuery.fn.extend和jQuery.extend

    extend 方法在 jQuery 中是一个很重要的方法,jQuey 内部用它来扩展静态方法或实例方法,而且我们开发 jQuery 插件开发的时候也会用到它.但是在内部,是存在 jQuery.fn.e ...