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. 原生js实现放大镜效果

    今天做任务的时候,有一个任务就是让实现电商网站常用的放大镜效果,类似于这样的效果,之前并没有做过这种放大镜效果,刚开始的思路是对图片进行裁剪,但是后来发现实在是难以实现,于是求助了万能的谷歌,发现一个 ...

  2. vim自动补全功能

    1.首先下载一个插件:ctags 输入:sudo apt-get install ctags 2.Ctrl+n进行单词的自动补全

  3. Android内存优化之 LruCache与DiskLruCache

    在日常的Adroid开发中我们经常遇到需要处理大量图片的地方,但Android手机的内存有限该怎么避免手机 内存溢出导致app程序oom,google提供了两种解决方式 LruCache LruCac ...

  4. Oracle查询数据库中的所有表

    SELECT A.TABLE_NAME 表英文名,       A.TAB_COMMENTS 表中文名,       A.COLUMN_ID 序号,       A.COLUMN_NAME 英文名,  ...

  5. 如何查看前端部署的tracker代码

    1.www.gov.cn 2.F12>Source>左侧选择static.gridsumdissector.com/js 3.点击代码下方区域的中括号,展开代码preety print{}

  6. Oracle中的MD5加密详解

    一.技术点 1. DBMS_OBFUSCATION_TOOLKIT.MD5 DBMS_OBFUSCATION_TOOLKIT.MD5是MD5编码的数据包函数,但偶在使用select DBMS_OBFU ...

  7. 64位系统下找不到office 32位组件

    如果系统式64位的,而装的是32位的office软件,在运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,但是却发现找不到Microsoft Excel程序, 这主要是64位系统的问题,exc ...

  8. android 返回键 操作

    cocos2dx项目移植到android平台上对于 android手机返回键,主菜单键等键的相关操作,本篇详细对返回键做个简单的介绍说明, 不足不对之处,请同猿们指出. 首先在主activity下,即 ...

  9. linux部分系统信息命令

    arch  显示机器的处理器架构9(1) uname -m 显示机器处理器架构(2) uname -r显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 hdparm-i/dev/hb ...

  10. 10 条有趣的 Linux 命令

    在终端工作是一件很有趣的事情.今天,我们将会列举一些有趣得为你带来欢笑的Linux命令. 1. rev 创建一个文件,在文件里面输入几个单词,rev命令会将你写的东西反转输出到控制台. # rev & ...