/**
* Source : https://oj.leetcode.com/problems/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 SearchMatrix { /**
* 因为矩阵是连续有序的,所以可以当做一维数组处理,使用二分法搜索
* 也可以使用二分法先搜索第一列,确定处于哪一行,再对该行使用二分法搜索
*
*
*
* @return
*/
public boolean search (int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int left = 0;
int right = m * n;
int mid = (left + right) / 2;
int midi = mid / n;
int midj = mid % n;
while (left <= right) {
if (matrix[midi][midj] == target) {
return true;
}
if (matrix[midi][midj] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
mid = (left + right) / 2;
midi = mid / n;
midj = mid % m;
}
return false;
} public static void main(String[] args) {
SearchMatrix searchMatrix = new SearchMatrix();
int[][] matrix = new int[][]{
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 50}
};
System.out.println(searchMatrix.search(matrix, 3));
System.out.println(searchMatrix.search(matrix, 11));
System.out.println(searchMatrix.search(matrix, 34));
System.out.println(searchMatrix.search(matrix, 0)); }
}

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高级程序设计~读书笔记

    1.函数-函数声明和函数表达式 解析器在向执行环境加载数据时,函数声明和函数表达式的对待不同. 解析器会率先执行函数声明,将会在任何使用到它的地方前加载, 而对于函数表达式,只会在执行到的时候去加载: ...

  2. bond绑定两张物理网卡为一张逻辑网卡

    问题:cnetos7同时接入两个独立网络,但两个网络的IP网段相同时只能路由到一个网络 解决方法:使用bond绑定两张物理网卡为一张逻辑网卡 1.新建文件bond.conf,内容如下 alias bo ...

  3. fortran语言调用fortran写的dll

    环境:vs2013+IVF 2011 有时候想把fortran写的常用的函数编译为DLL,以供不同的fortran exe调用,这时候应该怎样做呢?[参考 彭国伦老师 fortran95 程序设计 书 ...

  4. redis_哈希对象

    redis哈希对象的底层编码有两种:ziplist.hashtable ziplist编码 当一个哈希键只包含少量kv对.且key和value都是小整数值.短字符串时,redis会使用压缩列表来做 z ...

  5. Anfora 自动装载类

    1.目录结构 + Anfora | + Autoload | - ClassLoader.php | - Autoload.php - autoload.php 2.注册类加载器 src/Anfora ...

  6. python基础自学 第二天

    注释 分类 单行注释 多行注释 作用 使用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序可读性 单行注释(行注释) 以 # 开头,#右边所有的东西就被当成说明文字,而不是要执行的程序,只是说 ...

  7. swarm集群日常部分操作

    docker swarm: 1)查看集群使用docker info 或 docker node ls 查看集群中的相关信息 2)swarm集群中node的availability状态可以为 activ ...

  8. 关于isNaN()函数的细节

    根据<JavaScript高级程序设计>的解释,NaN,即非数值(Not a Number),用于表示一个本来要返回数值的操作数未返回数值的情况,例如5/0就会得到NaN. 而因为NaN的 ...

  9. 网络编程懒人入门(九):通俗讲解,有了IP地址,为何还要用MAC地址?

    1.前言 标题虽然是为了解释有了 IP 地址,为什么还要用 MAC 地址,但是本文的重点在于理解为什么要有 IP 这样的东西.本文对读者的定位是知道 MAC 地址是什么,IP 地址是什么. (本文同步 ...

  10. InnoDB Insert Buffer(插入缓冲)

    InnoDB Insert Buffer(插入缓冲) 每个存储存储引擎自身都有自己的特性(决定性能以及更高可靠性),而InnoDB的关键特性有: 插入缓冲(Insert Buffer)-->Ch ...