Skip to content

Commit 07bcf9e

Browse files
author
王鹏
committed
feat(MEDIUM): add _74_searchMatrix
1 parent 36c3399 commit 07bcf9e

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package pp.arithmetic.leetcode;
2+
3+
/**
4+
* Created by wangpeng on 2019-10-24.
5+
* 74. 搜索二维矩阵
6+
*
7+
* 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
8+
*
9+
* 每行中的整数从左到右按升序排列。
10+
* 每行的第一个整数大于前一行的最后一个整数。
11+
* 示例 1:
12+
*
13+
* 输入:
14+
* matrix = [
15+
* [1, 3, 5, 7],
16+
* [10, 11, 16, 20],
17+
* [23, 30, 34, 50]
18+
* ]
19+
* target = 3
20+
* 输出: true
21+
* 示例 2:
22+
*
23+
* 输入:
24+
* matrix = [
25+
* [1, 3, 5, 7],
26+
* [10, 11, 16, 20],
27+
* [23, 30, 34, 50]
28+
* ]
29+
* target = 13
30+
* 输出: false
31+
*
32+
* 来源:力扣(LeetCode)
33+
* 链接:https://leetcode-cn.com/problems/search-a-2d-matrix
34+
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
35+
*/
36+
public class _74_searchMatrix {
37+
38+
public static void main(String[] args) {
39+
int[][] matrix = new int[][]{
40+
{1, 3, 5, 7},
41+
{10, 11, 16, 20},
42+
{23, 30, 34, 50}
43+
};
44+
_74_searchMatrix searchMatrix = new _74_searchMatrix();
45+
System.out.println(searchMatrix.searchMatrix(matrix,1));
46+
System.out.println(searchMatrix.searchMatrix(matrix,3));
47+
System.out.println(searchMatrix.searchMatrix(matrix,5));
48+
System.out.println(searchMatrix.searchMatrix(matrix,7));
49+
System.out.println(searchMatrix.searchMatrix(matrix,10));
50+
System.out.println(searchMatrix.searchMatrix(matrix,11));
51+
System.out.println(searchMatrix.searchMatrix(matrix,16));
52+
System.out.println(searchMatrix.searchMatrix(matrix,20));
53+
System.out.println(searchMatrix.searchMatrix(matrix,23));
54+
System.out.println(searchMatrix.searchMatrix(matrix,50));
55+
System.out.println(searchMatrix.searchMatrix(matrix,13));
56+
System.out.println(searchMatrix.searchMatrix(matrix,40));
57+
}
58+
59+
/**
60+
* 解题思路:
61+
* 整个矩阵类似一个有序的升序数组,考虑使用二分查找是否存在目标值
62+
* 难点:中间点的计算
63+
* 1.利用公式计算起点和终点之间的差:(ex - sx) * col + ey - sy,
64+
* 2.中间点距离起点的步数:ml = ((ex - sx) * col + ey - sy) / 2
65+
* 3.mx = sx + (ml + sy) / col <== 起点+偏移计算出中间点的x
66+
* 4.my = ml + sy - (mx - sx) * col <== 根据第一步的公式,代入mx,计算出my
67+
* 5.利用二分查找的规则判断出结果
68+
*
69+
* 执行用时 :0 ms, 在所有 java 提交中击败了100.00%的用户
70+
* 内存消耗 :42.6 MB, 在所有 java 提交中击败了39.56%的用户
71+
*
72+
* @param matrix
73+
* @param target
74+
* @return
75+
*/
76+
public boolean searchMatrix(int[][] matrix, int target) {
77+
if (matrix == null || matrix.length == 0) return false;
78+
int row = matrix.length;
79+
int col = matrix[0].length;
80+
if (col == 0) return false;
81+
int sx = 0, sy = 0, ex = row - 1, ey = col - 1;
82+
if (target < matrix[sx][sy] || target > matrix[ex][ey]) return false;
83+
int mx, my, ml;
84+
while (sx * col + sy <= ex * col + ey) {
85+
//计算中间点
86+
ml = ((ex - sx) * col + ey - sy) / 2;
87+
mx = sx + (ml + sy) / col;
88+
my = ml + sy - (mx - sx) * col;
89+
int middle = matrix[mx][my];
90+
if (middle == target) return true;
91+
//防止无法退出
92+
if (ml == 0) {
93+
if (matrix[ex][ey] == target) return true;
94+
return false;
95+
}
96+
if (middle > target) {
97+
ex = mx;
98+
ey = my;
99+
} else {
100+
sx = mx;
101+
sy = my;
102+
}
103+
}
104+
105+
return false;
106+
}
107+
}

0 commit comments

Comments
 (0)