Question: 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.

Analysis:

写出一个算法,能够找出一个m * n矩阵中是否含一个数。这个m*n的矩阵有如下性质:

1. 每行的整数从左到右是有序的;

2. 每行的第一个整数要比上一行的最后一个整数大。

思路:

不论是单行有序或整体有序,都可以使用这样的策略解决问题:

从矩阵的右上角开始寻找,如果target比他大,则往下找;如果target比他小,则往左找,如果矩阵中确实存在这个数,则一定能找到,若找到最后也没有找到,则返回false。

这道题目还可以用二分法解决。用两次二分法,先找到行,再找到列。(但是对于单行有序的题目来说则不适合解决)

Answer:

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
return false;
int row = matrix.length;
int col = matrix[0].length;
int i = 0;
int j = matrix[0].length-1;
while(i < matrix.length && j >= 0) {
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] < target)
i++;
else if(matrix[i][j] > target)
j--;
}
return false;
}
}
 Question: Search a 2D Matrix II

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 in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

For example,

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

Analsyis:

这个题目与上面不同的是,矩阵的性质是:

1. 每行从走到有是有序的;

2. 每列从上到下是有序的;

但满足这两个性质并不能保证这个矩阵整体是有序的,因此用二分法一定不能解决问题,但是我们可以使用上面从右上角开始,更新i,j的值来寻找矩阵中是否存在这个元素的方法。

Answer:

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
return false;
int row = matrix.length;
int col = matrix[0].length;
int i = 0;
int j = matrix[0].length-1;
while(i < matrix.length && j >= 0) {
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] < target)
i++;
else if(matrix[i][j] > target)
j--;
}
return false;
}
}
 

LeetCode -- Search a 2D Matrix & Search a 2D Matrix II的更多相关文章

  1. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  2. [Leetcode] Binary search, Divide and conquer--240. 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 ...

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

  4. [leetcode] Add to List 74. Search a 2D Matrix

    /** * Created by lvhao on 2017/8/1. * Write an efficient algorithm that searches for a value in an m ...

  5. sorted matrix - search & find-k-th

    sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right ...

  6. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  7. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  8. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  9. Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of eff ...

随机推荐

  1. js延迟执行与循环执行

    延迟一段时间执行特定代码: setTimeout(function () { window.location.href = 'login' },1200); 循环执行: function test() ...

  2. linux下安装xtrabackup

    下载需要的版本的xtrabackup软件包,链接如下: https://www.percona.com/downloads/XtraBackup/LATEST/ percona-xtrabackup- ...

  3. linux系统ext文件系统知识

    ext2文件系统细节 我们都知道,操作系统中的数据分为文件内容和文件属性两部分,其中文件内容就是文件的实体数据,而文件属性就是文件类型.权限.属主.修改时间等信息.操作系统会将上述文件的内容放入磁盘文 ...

  4. scrapy--json(360美图)

    之前开始学习scrapy,接触了AJax异步加载.一直没放到自己博客,趁现在不忙,也准备为下一个爬虫做知识储存,就分享给大家. 还是从爬取图片开始,先上图给大家看看成果,QAQ. 一.图片加载的方法 ...

  5. 小游戏banner广告流量量主指引

    小程序导航 https://wq.xmaht.top

  6. php扩展开发-INI配置

    php.ini文件是用来保存各项扩展配置的文件,每个扩展都或多或少需要有一个定制化的配置,ini文件是一个很好的保存配置的方式,我们来看下怎么在自己的扩展里,使用到ini的配置功能 //创建ini的配 ...

  7. 学习Pytbon第十七篇,面向对象编程

    面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类 ...

  8. dijkstra算法与优先队列

    这是鄙人的第一篇技术博客,作为算法小菜鸟外加轻度写作障碍者,写技术博客也算是对自己的一种挑战和鞭策吧~ 言归正传,什么是dijkstra算法呢? -dijkstra算法是一种解决最短路径问题的简单有效 ...

  9. Android 第三方库RxLifecycle使用

    1.简单介绍RxLifecycle 1.1.使用原因. 在使用rxjava的时候,如果没有及时解除订阅,在退出activity的时候,异步线程还在执行. 对activity还存在引用,此时就会产生内存 ...

  10. 1,VMware与Centos系统安装

    选择性 pc可以选择 -纯系统 Linux/windows -双系统 Windows+Linux -虚拟化技术 Windows+vmware workstation 服务器 -物理机纯系统 -物理机+ ...