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复习,预编译

    注意:函数声明整体提升.变量 声明提升 1.imply global 暗示全局变量:即任何变量,如果变量未声明就赋值,此变量就为全局对象所有 ==>  eg: a = 122;==>  e ...

  2. python基础数据类型之列表,元组操作

    一.列表的索引和切片1.列表的索引列表和字符串一样样拥有索引 lst = ["a","b","c"] print(lst[0]) # 获取第 ...

  3. Windows下MySQL数据库的安装与关闭开机自启动

    我在学习java,安装数据库时找了很多教程,现在在这里总结一下我安装数据库的过程,我安装的是mysql-5.6.42-winx64版本的. 数据官方下载地址:https://dev.mysql.com ...

  4. MySql开启GTID和多线程复制功能

    1.修改参数 master: gtid_mode = ON                        --开启gtid这个必须打开 enforce-gtid-consistency = ON    ...

  5. 【JavaScript高级程序设计】6.1 理解对象

    上一章曾经介绍过,创建自定义对象的最简单方式就是创建一个Object 的实例,然后再为它添加属性和方法,如下所示. var person = new Object(); person.name = & ...

  6. Codeforces Round #449 (Div. 2) C. DFS

    C. Nephren gives a riddle time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. django-mysqlclient_1193错误

    Django 配置 mysqlclient 驱动 1193错误 错误描述: django.db.utils.OperationalError: (1193, "Unknown system ...

  8. java线程安全总结 - 2 (转载)

    原文地址:http://www.jameswxx.com/java/%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E6%80%BB%E7%BB%93%EF%BC%88%E4 ...

  9. 清空Fragment回退栈中某个Fragment之上的所有Fragment

    根据debug信息查看Fragment回退栈的情况,具体debug代码如下: int num = getActivity().getSupportFragmentManager().getBackSt ...

  10. 15.3,redis持久化RDB与AOF

    redis持久化 Redis是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失,为了解决这个问题,Redis提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的丢失. RDB持久化 ...