-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0074_SearchA2dMatrix.java
More file actions
89 lines (86 loc) · 2.77 KB
/
P0074_SearchA2dMatrix.java
File metadata and controls
89 lines (86 loc) · 2.77 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package yyl.leetcode.p00;
/**
* <h3>搜索二维矩阵</h3><br>
* 编写一个高效的算法来判断 m*n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:<br>
* 每行中的整数从左到右按升序排列。<br>
* 每行的第一个整数大于前一行的最后一个整数。<br>
*
* <pre>
* 示例 1:
* 输入:
* matrix = [
* [1, 3, 5, 7],
* [10, 11, 16, 20],
* [23, 30, 34, 50]
* ]
* target = 3
* 输出: true
*
* 示例 2:
* 输入:
* matrix = [
* [1, 3, 5, 7],
* [10, 11, 16, 20],
* [23, 30, 34, 50]
* ]
* target = 13
* 输出: false
* </pre>
*/
public class P0074_SearchA2dMatrix {
public static void main(String[] args) {
Solution solution = new Solution();
int[][] matrix = {//
{1, 3, 5, 7}, //
{10, 11, 16, 20}, //
{23, 30, 34, 50},//
};
int target = 3;
System.out.println(solution.searchMatrix(matrix, target));
}
// 二分法查找
// 根据题目矩阵特征,每行的元素都是升序,可以将输入的 m*n矩阵可以视为长度为 m*n的有序数组,然后使用二分法来查找
// 时间复杂度:O(log(M*N)),标准的二分查找
// 空间复杂度:O(1)
static class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int columns = matrix[0].length;
int low = 0;
int high = matrix.length * columns - 1;
while (low <= high) {
int middle = (low + high) / 2;
int value = matrix[middle / columns][middle % columns];
if (target == value) {
return true;
} else if (target > value) {
low = middle + 1;
} else /* if (target < value) */ {
high = middle - 1;
}
}
return false;
}
}
// 直接循环遍历
// 时间复杂度:O(M*N),M和N是矩阵的宽高
// 空间复杂度:O(1)
static class Solution2 {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int n = matrix[0].length;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == target) {
return true;
}
}
}
return false;
}
}
}