/**
* 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. while read line 查找指定日期的文件夹

    #!/bin/bash dir=/root/tmp targetdir=/root/tmp/tmp filenametxt=/tmp/filename.txt commandtxt=/tmp/comm ...

  2. 只要一行代码求一串字符中某字符(串)出现次数,c#

    这里只要一行代码就行. static void Main(string[] args) { string str = "qwerwqr;sfdsfds;fdfdsf;dfsdfsdf;dsf ...

  3. Exp2 后门原理与实践-------20164325王晓蕊

    Exp2 后门原理与实践 任务一:使用netcat获取主机操作Shell,cron启动 1.windows获取linux shell 1.1 ipconfig  查看本机查看以太网适配器(8)的ipv ...

  4. Shader_ShaderForge_NGUI_序列帧/

    序列帧 Shader篇 Shader Forge序列帧算法! 附上Shader代码部分: // Shader created with Shader Forge v1.26 // Shader For ...

  5. JavaScript 中的正则表达式

    1.正则表达式概述 ECMAScript 3 开始支持正则表达式,其语法和 Perl 语法很类似,一个完整的正则表达式结构如下: 1 var expression = / pattern / flag ...

  6. IntelliJ IDEA小问题通过操作软件解决

    Diamond types are not supported at this language level http://blog.csdn.net/qq_34884729/article/deta ...

  7. 常用maven仓库

    常用Maven仓库网址:http://mvnrepository.com/http://search.maven.org/http://repository.sonatype.org/content/ ...

  8. 学习Axure RP原型设计

    1 概述 原型设计是应用开发设计的第一要素.好的原型设计不仅可以起到沟通的作用,而且对客户而言应用程序拥有更直观的体现.原型设计通过内容和结构展示以及界面布局编排,实现在开发前期用户与产品进行交互.提 ...

  9. Android JNI 学习(二):JNI 设计机制

    本章我们重点说明以下JNI设计的问题,本章中提到的大多数设计问题都与native方法有关.至于调用相关的API的设计,我们会在后面进行介绍. 一.JNI接口函数和指针 native 代码通过调用JNI ...

  10. MySQL 每秒 570000 的写入,如何实现?

    阅读本文大概需要 2.8 分钟. 来源:http://t.cn/E2TbCg5 一.需求 一个朋友接到一个需求,从大数据平台收到一个数据写入在20亿+,需要快速地加载到MySQL中,供第二天业务展示使 ...