forked from haobinaa/DataStructure-DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumInDimensionalArray.java
64 lines (61 loc) · 2.01 KB
/
NumInDimensionalArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.haobin.offer;
/**
* @author: HaoBin
* @create: 2019/9/17 10:27
* @description: 4- 判断数是否在二维数组中
* 题目描述: 给定一个二维数组,其每一行从左到右递增排序,
* 从上到下也是递增排序。给定一个数,判断这个数是否在该二维数组中
* 要求时间复杂度 O(M + N),空间复杂度 O(1)。其中 M 为行数,N 为 列数。
* <p>
* eg:
* 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]
* ]
* <p>
* Given target = 5, return true.
* Given target = 20, return false.
**/
public class NumInDimensionalArray {
/**
* 思路: 该二维数组中的一个数,小于它的数一定在其左边,大于它的数一定在其下边。
* 因此,从右上角开始查找,就可以根据 target 和当前元素的大小关系来缩小查找区间,
* 当前元素的查找区间为左下角的所有元素
*
* @param args
*/
public static void main(String[] args) {
int[][] array = {{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}};
System.out.println(find(7, array));
}
/**
* @param target 目标数字
* @param matrix 给定二维数组
* @return 是否在二维数组中
*/
public static boolean find(int target, int[][] matrix) {
if (matrix.length == 0) {
return false;
}
int row = matrix.length;
int col = matrix[0].length;
int r = 0, c = col - 1; // 从右上角开始
// 一直缩小区间到左下角
while (r <= row - 1 && c >= 0) {
if (target == matrix[r][c]) {
return true;
} else if (target > matrix[r][c]) {
// 向下探索
r++;
} else if (target < matrix[r][c]) {
// 向左探索
c--;
}
}
return false;
}
}