Skip to content

Commit e6cd236

Browse files
committed
二刷74
1 parent b4c2860 commit e6cd236

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

docs/0074-search-a-2d-matrix.adoc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[#0074-search-a-2d-matrix]
22
= 74. 搜索二维矩阵
33

4-
https://leetcode.cn/problems/search-a-2d-matrix/[LeetCode - 74. 搜索二维矩阵 ^]
4+
https://leetcode.cn/problems/search-a-2d-matrix/[LeetCode - 74. 搜索二维矩阵^]
55

66
给你一个满足下述两条属性的 `m x n` 整数矩阵:
77

@@ -28,7 +28,6 @@ image::images/0074-02.jpg[{image_attr}]
2828
输出:false
2929
....
3030

31-
3231
*提示:*
3332

3433
* `m == matrix.length`
@@ -39,7 +38,7 @@ image::images/0074-02.jpg[{image_attr}]
3938
4039
== 思路分析
4140

42-
由于矩阵同行有序,下一行比上一行大,所以,把每一行“拼接”起来就是一个有序数组,可以用二分查找解决问题。
41+
由于矩阵同行有序,下一行比上一行大,所以,通过将矩阵容量转换成坐标,把每一行“拼接”起来就是一个有序数组,可以用二分查找解决问题。
4342

4443
[[src-0074]]
4544
[tabs]
@@ -53,17 +52,20 @@ include::{sourcedir}/_0074_SearchA2DMatrix.java[tag=answer]
5352
----
5453
--
5554
56-
// 二刷::
57-
// +
58-
// --
59-
// [{java_src_attr}]
60-
// ----
61-
// include::{sourcedir}/_0074_SearchA2DMatrix_2.java[tag=answer]
62-
// ----
63-
// --
55+
二刷::
56+
+
57+
--
58+
[{java_src_attr}]
59+
----
60+
include::{sourcedir}/_0074_SearchA2DMatrix_2.java[tag=answer]
61+
----
62+
--
6463
====
6564

6665

6766
== 参考资料
6867

68+
. https://leetcode.cn/problems/search-a-2d-matrix/solutions/2783931/liang-chong-fang-fa-er-fen-cha-zhao-pai-39d74/[74. 搜索二维矩阵 - 两种方法:二分查找/排除法^]
69+
. https://leetcode.cn/problems/search-a-2d-matrix/solutions/688573/gong-shui-san-xie-yi-ti-shuang-jie-er-fe-l0pq/[74. 搜索二维矩阵 - 一题双解:「二分」&「抽象 BST」解法^]
70+
. https://leetcode.cn/problems/search-a-2d-matrix/solutions/688623/fu-xue-ming-zhu-liu-chong-fang-fa-bang-n-e20z/[74. 搜索二维矩阵 - 六种方法,帮你拓展思路^]
6971
. https://leetcode.cn/problems/search-a-2d-matrix/solutions/688117/sou-suo-er-wei-ju-zhen-by-leetcode-solut-vxui/[74. 搜索二维矩阵 - 官方题解^]

docs/0240-search-a-2d-matrix-ii.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ include::{sourcedir}/_0240_SearchA2DMatrixIi_3.java[tag=answer]
101101

102102
== 参考资料
103103

104-
. https://leetcode.cn/problems/search-a-2d-matrix-ii/solutions/2361487/240-sou-suo-er-wei-ju-zhen-iitan-xin-qin-7mtf/?envType=study-plan-v2&envId=selected-coding-interview[240. 搜索二维矩阵 II - 贪心,清晰图解^]
105-
. https://leetcode.cn/problems/search-a-2d-matrix-ii/solutions/2783938/tu-jie-pai-chu-fa-yi-tu-miao-dong-python-kytg/?envType=study-plan-v2&envId=selected-coding-interview[240. 搜索二维矩阵 II - 【图解】排除法,一图秒懂!^]
106-
. https://leetcode.cn/problems/search-a-2d-matrix-ii/solutions/1062538/sou-suo-er-wei-ju-zhen-ii-by-leetcode-so-9hcx/?envType=study-plan-v2&envId=selected-coding-interview[240. 搜索二维矩阵 II - 官方题解^]
104+
. https://leetcode.cn/problems/search-a-2d-matrix-ii/solutions/2361487/240-sou-suo-er-wei-ju-zhen-iitan-xin-qin-7mtf/[240. 搜索二维矩阵 II - 贪心,清晰图解^]
105+
. https://leetcode.cn/problems/search-a-2d-matrix-ii/solutions/2783938/tu-jie-pai-chu-fa-yi-tu-miao-dong-python-kytg/[240. 搜索二维矩阵 II - 【图解】排除法,一图秒懂!^]
106+
. https://leetcode.cn/problems/search-a-2d-matrix-ii/solutions/1062538/sou-suo-er-wei-ju-zhen-ii-by-leetcode-so-9hcx/[240. 搜索二维矩阵 II - 官方题解^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,11 @@ endif::[]
19391939
|{doc_base_url}/0075-sort-colors.adoc[题解]
19401940
|✅ 计数排序或三路快排。
19411941

1942+
|{counter:codes2503}
1943+
|{leetcode_base_url}/search-a-2d-matrix/[74. 搜索二维矩阵^]
1944+
|{doc_base_url}/0074-search-a-2d-matrix.adoc[题解]
1945+
|✅ 二分查找。通过将矩阵容量转换成坐标,把矩阵按行“拼接”,使用二分查找来处理,解法非常妙。
1946+
19421947
|===
19431948

19441949
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0074_SearchA2DMatrix_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-11-18 22:23:27
8+
*/
9+
public boolean searchMatrix(int[][] matrix, int target) {
10+
int m = matrix.length;
11+
int n = matrix[0].length;
12+
int low = 0, high = m * n - 1;
13+
while (low <= high) {
14+
int mid = low + (high - low) / 2;
15+
int row = mid / n;
16+
int col = mid % n;
17+
if (matrix[row][col] == target) {
18+
return true;
19+
} else if (matrix[row][col] < target) {
20+
low = mid + 1;
21+
} else {
22+
high = mid - 1;
23+
}
24+
}
25+
return false;
26+
}
27+
// end::answer[]
28+
}

0 commit comments

Comments
 (0)