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. java基础必备单词讲解 day two

    variable 变量 count 统计 sum 总数 salary 薪水 Scanner 接收 import 导入 eclipse 日食 control 控制 shift 改变 alt 替换键 ha ...

  2. Xcode 中 pch 文件配置 - iOS

    一.简介 首先 pch 文件(即:Prefix Header)是一种预编译文件,在 Xcode 6 之前创建新的工程则会自动将该文件一起创建出来,但在 Xcode 6 之后苹果官方则默认将自动创建的方 ...

  3. jQuery选择器与事件学习笔记

    层次选择器:  $("div li")获取div下的所有li元素(后代.子.子的子......)  $("div>li")获取div下的直接li子元素.  ...

  4. SAP标准导出功能 - 删除默认选定格式

    我们经常会使用SAP系统的标准功能导出ALV显示的数据,一般会选择电子表格. 选择电子表格之后,需要选择电子表格的具体格式. 选择格式之后点击确定,会弹出保存对话框. 如果在使用这个功能的时候,选择了 ...

  5. Spring Cloud 入门 Eureka-Client服务提供

    前面文章介绍了如果创建“服务注册中心”,现在创建“服务提供者”,并向服务注册中心注册自己,在服务提供方中尝试着提供一个接口来获取当前所有的服务信息. 先,创建一个基本的Spring Boot应用.命名 ...

  6. 屏蔽datatable错误提示

    $.fn.dataTable.ext.errMode = 'none'; //不显示任何错误信息// 以下为发生错误时的事件处理,如不处理,可不管.$('#productionRequestItems ...

  7. Docker学习之镜像操作

    使用Docker镜像 以下都是Docker镜像的一系列重要名操作,包括获取.查看.搜索.删除.创建.存出或载入.上传等.可使用docker image help命令查看帮助. 1.获取镜像(pull) ...

  8. 本地通过VMware Workstation创建虚拟机,配置网络环境

    通过VMware Workstation创建虚拟机,系统安装完成后,需要配置相应网卡设置: 打开配置文件:vim /etc/sysconfig/network-scripts/ifcfg-ens33 ...

  9. GDOI--DAY2 游记

    今天,熬夜不够多,果然,不出所料,爆零了... 第一题,看到数据之大,懵逼了,于是,敲了个二分SPFA,但是!最大的点GG了,呜呜~~~~(>_<)~~~~ ,于是,就不继续做第一题了(虽 ...

  10. C语言函数篇(四)函数的设计

    1. 函数设计的时候,如果使用到全局变量,就尽量通过参数的形式传递进来 也就是说,保持 函数 跟 外部的交互 只有 参数 和 返回值 2. 在有参数的情况下,或者有数值输入的时候,要先进行错误判断. ...