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

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

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

  3. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  4. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

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

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

  7. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

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

  9. 074 Search a 2D Matrix 搜索二维矩阵

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行中的整数从左到右排序.    每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[  [1,   3, ...

随机推荐

  1. config 写入

    d代码: # __author__ = liukun # coding:utf-8 line = input("please input the config \n :") #提示 ...

  2. 轻量级应用开发之(08)UITableView

    一  UITableView基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UISc ...

  3. POJ1328Radar Installation(区间点覆盖问题)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68597   Accepted: 15 ...

  4. HD2255奔小康赚大钱(最大权匹配模板)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. 使用Navicat V8.0创建数据库,外键出现错误ERROR 1005: Can’t create table (errno: 121)

    ERROR 1005: Can't create table (errno: 121) errno 121 means a duplicate key error. Probably the tabl ...

  6. Linux命令详解:[7]获得命令帮助

    在维护和使用Linux系统时,常常会忘记命令的使用方法,如果旁边又没有相应的资料,那怎么办呢?不用担心,系统本身提供了详细的手册供使用者查询,下面小编就以CentOS6.4系统为例演示如何获得命令帮助 ...

  7. 深入了解A*

    一.前言 在这里我将对A*算法的实际应用进行一定的探讨,并且举一个有关A*算法在最短路径搜索的例子.值得注意的是这里并不对A*的基本的概念作介绍,如果你还对A*算法不清楚的话,请看姊妹篇<初识A ...

  8. 织梦DedeCMS删除所有栏目或文章后,新建ID不从1开始的解决方法

    这个修改方法很简单,从模板无忧那里找到的,只需要在后台系统-SQL命令行工具里面运行以下语句即可,不用采用笨方法重新安装织梦CMS了. 删除所有栏目,新建ID从1开始: ALTER TABLE `de ...

  9. goquery

    使用goquery 会用jquery的,goquery基本可以1分钟上手,下面是goquery文档 http://godoc.org/github.com/PuerkitoBio/goquery 1. ...

  10. Linux下/proc目录简介

    文章转载至:http://blog.csdn.net/zdwzzu2006/article/details/7747977 1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在 ...