Search a 2D Matrix II

简介: 搜索2维数组;如果数组中存在目标值,返回true,否则返回false Write an efficient algorithm that searches for a value in an m x n matrix.

搜索2维数组;如果数组中存在目标值,返回true,否则返回false

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.

遍历的话肯定效率低下。设立游标移动搜索。那么问题是从哪里开始搜索?

比较好的办法是从右上角开始。如果右上角的数字大于target,那么这一列没有target;向左一列移动

如果小于target,那么这一行没有target,向下一行移动

搜索停止的条件就是越界

 

Java代码:

 1 package com.rust.cal;
 2 
 3 public class Searcha2DMatrixII {
 4 
 5     public static boolean searchMatrix(int[][] matrix, int target) {
 6         if (matrix.length == 0 || matrix[0].length == 0) {
 7             return false;
 8         }
 9         int dy = matrix[0].length - 1;
10         int delta = matrix[0][dy];
11         int dx = 0;
12         while(dx < matrix.length && dy >= 0){
13             if (matrix[dx][dy] == target) {
14                 return true;
15             } else if (matrix[dx][dy] < target) {
16                 dx++;
17             } else {
18                 dy--;
19             }
20         }
21         return false;
22     }
23     public static void main(String args[]){
24         int matrix[][] = {
25                 {1 ,2 ,3 ,4 ,9 },
26                 {6 ,7 ,8 ,10,19},
27                 {17,18,30,31,32}
28         };
29         
30         int matrix1[][] = {
31                 {1,2},
32                 {4,5},
33                 {6,17},
34                 {10,20}
35         };
36         
37         int matrix2[][] = {
38                 {1,3,5,7,9,13,17},
39                 {2,4,6,8,10,16,20}
40         };
41         
42         System.out.println("matrix has 17?  " + searchMatrix(matrix, 17));
43         System.out.println("matrix has 18?  " + searchMatrix(matrix, 18));
44         System.out.println("matrix has 19?  " + searchMatrix(matrix, 19));
45         System.out.println("matrix1 has 4?  " + searchMatrix(matrix1, 4));
46         System.out.println("matrix2 has 4?  " + searchMatrix(matrix2, 4));
47         
48     }
49 }

输出:

matrix has 17?  true
matrix has 18?  true
matrix has 19?  true
matrix1 has 4?  true
matrix2 has 4?  true

 

目录
相关文章
Leetcode 74. Search a 2D Matrix
这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。 假设矩阵是m*n,扫一遍的时间复杂度就是O(m*n),题目中给出的这么特殊的矩阵,时间复杂度可以降到O(m+n),具体代码如下,写的比较挫。
86 1
Leetcode 240. Search a 2D Matrix II
具体思路就是每一行倒着扫,扫到第一个比target小的数就跳到下行,如果等于当然是直接返回true了,如果下一行还比target小就继续跳下一行,直到最后一行。 为啥这么做是可行的? 可能我比较笨,想了半天才想到。 因为每一列都是增序的,举个例子,假设matrix[0][5] > target,那么[0][5]位置右下(包含右和下)所有元素不可能比target小。
47 0
|
算法
LeetCode 240. Search a 2D Matrix II
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性: 每行的元素从左到右升序排列。 每列的元素从上到下升序排列。
82 0
LeetCode 240. Search a 2D Matrix II
|
算法
LeetCode 74. Search a 2D Matrix
编写一个有效的算法,搜索m×n矩阵中的值。 此矩阵具有以下属性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.
81 0
LeetCode 74. Search a 2D Matrix
|
索引
LeetCode 81. Search in Rotated Sorted Array II
假设按升序排序的数组在事先未知的某个枢轴处旋转。 (例如, [0,0,1,2,2,5,6] 可能变成 [2,5,6,0,0,1,2]). 给定一个要搜索的目标值T, 如果该目标值能在数组中找到则返回true,否则返回false。
91 0
LeetCode 81. Search in Rotated Sorted Array II
[LeetCode]--74. 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
1170 0
LeetCode - 34. Search for a Range
34. Search for a Range  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个有序数组和一个数k,求k在这个数组中的起始下标和结束下标.
897 0
[LeetCode] 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
1221 0