Skip to content

Commit 0d909c0

Browse files
committed
一刷74
1 parent b139304 commit 0d909c0

File tree

8 files changed

+100
-36
lines changed

8 files changed

+100
-36
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
544544
|Medium
545545
|
546546

547-
//|{counter:codes}
548-
//|{leetcode_base_url}/search-a-2d-matrix/[74. Search a 2D Matrix^]
549-
//|{source_base_url}/_0074_SearchA2DMatrix.java[Java]
550-
//|{doc_base_url}/0074-search-a-2d-matrix.adoc[题解]
551-
//|Medium
552-
//|
547+
|{counter:codes}
548+
|{leetcode_base_url}/search-a-2d-matrix/[74. Search a 2D Matrix^]
549+
|{source_base_url}/_0074_SearchA2DMatrix.java[Java]
550+
|{doc_base_url}/0074-search-a-2d-matrix.adoc[题解]
551+
|Medium
552+
|
553553

554554
|{counter:codes}
555555
|{leetcode_base_url}/sort-colors/[75. Sort Colors^]

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

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

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

6-
Write an efficient algorithm that searches for a value in an _m_ x _n_ matrix. This matrix has the following properties:
6+
给你一个满足下述两条属性的 `m x n` 整数矩阵:
77

8+
* 每行中的整数从左到右按非严格递增顺序排列。
9+
* 每行的第一个整数大于前一行的最后一个整数。
810
9-
* Integers in each row are sorted from left to right.
10-
* The first integer of each row is greater than the last integer of the previous row.
11+
给你一个整数 `target` ,如果 `target` 在矩阵中,返回 `true`;否则,返回 `false`
1112

13+
*示例 1:*
1214

13-
*Example 1:*
15+
image::images/0074-01.jpg[{image_attr}]
1416

15-
[subs="verbatim,quotes,macros"]
16-
----
17-
*Input:*
18-
matrix = [
19-
[1, 3, 5, 7],
20-
[10, 11, 16, 20],
21-
[23, 30, 34, 50]
22-
]
23-
target = 3
24-
*Output:* true
17+
....
18+
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
19+
输出:true
20+
....
2521

26-
----
22+
*示例 2:*
2723

28-
*Example 2:*
24+
image::images/0074-02.jpg[{image_attr}]
2925

30-
[subs="verbatim,quotes,macros"]
31-
----
32-
*Input:*
33-
matrix = [
34-
[1, 3, 5, 7],
35-
[10, 11, 16, 20],
36-
[23, 30, 34, 50]
37-
]
38-
target = 13
39-
*Output:* false
40-
----
26+
....
27+
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
28+
输出:false
29+
....
30+
31+
32+
*提示:*
4133

34+
* `m == matrix.length`
35+
* `n == matrix[i].length`
36+
* `+1 <= m, n <= 100+`
37+
* `-10^4^ \<= matrix[i][j], target \<= 10^4^`
4238
4339
40+
== 思路分析
41+
42+
由于矩阵同行有序,下一行比上一行大,所以,把每一行“拼接”起来就是一个有序数组,可以用二分查找解决问题。
43+
4444
[[src-0074]]
45+
[tabs]
46+
====
47+
一刷::
48+
+
49+
--
4550
[{java_src_attr}]
4651
----
4752
include::{sourcedir}/_0074_SearchA2DMatrix.java[tag=answer]
4853
----
54+
--
55+
56+
// 二刷::
57+
// +
58+
// --
59+
// [{java_src_attr}]
60+
// ----
61+
// include::{sourcedir}/_0074_SearchA2DMatrix_2.java[tag=answer]
62+
// ----
63+
// --
64+
====
65+
66+
67+
== 参考资料
4968

69+
. https://leetcode.cn/problems/search-a-2d-matrix/solutions/688117/sou-suo-er-wei-ju-zhen-by-leetcode-solut-vxui/[74. 搜索二维矩阵 - 官方题解^]

docs/images/0074-01.jpg

13.2 KB
Loading

docs/images/0074-02.jpg

13 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ include::0072-edit-distance.adoc[leveloffset=+1]
230230

231231
include::0073-set-matrix-zeroes.adoc[leveloffset=+1]
232232

233-
// include::0074-search-a-2d-matrix.adoc[leveloffset=+1]
233+
include::0074-search-a-2d-matrix.adoc[leveloffset=+1]
234234

235235
include::0075-sort-colors.adoc[leveloffset=+1]
236236

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ endif::[]
463463
|{doc_base_url}/0032-longest-valid-parentheses.adoc[题解]
464464
|⭕️ 栈。使用栈记录左右括号的下标,匹配后标注“占位符”。最后,统计占位符中的数量,找出最大长度。
465465

466+
|{counter:codes2503}
467+
|{leetcode_base_url}/search-a-2d-matrix/[74. 搜索二维矩阵^]
468+
|{doc_base_url}/0074-search-a-2d-matrix.adoc[题解]
469+
|✅ 二分查找。把矩阵按行“拼接”,然后二分查找,解法非常妙。
470+
466471
|===
467472

468473
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0073_SetMatrixZeroes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class _0073_SetMatrixZeroes {
6161
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Set Matrix Zeroes.
6262
*
6363
* Memory Usage: 50.4 MB, less than 5.71% of Java online submissions for Set Matrix Zeroes.
64+
*
65+
* @author D瓜哥 · https://www.diguage.com
66+
* @since 2019-10-27 00:59
6467
*/
6568
public void setZeroes(int[][] matrix) {
6669
if (Objects.isNull(matrix) || matrix[0].length == 0) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0074_SearchA2DMatrix {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-04-23 18:20:50
9+
*/
10+
public boolean searchMatrix(int[][] matrix, int target) {
11+
int row = matrix.length;
12+
int column = matrix[0].length;
13+
int left = 0, right = row * column - 1;
14+
while (left <= right) {
15+
int mid = left + (right - left) / 2;
16+
int num = matrix[mid / column][mid % column];
17+
if (num == target) {
18+
return true;
19+
} else if (target < num) {
20+
right = mid - 1;
21+
} else {
22+
left = mid + 1;
23+
}
24+
}
25+
return false;
26+
}
27+
28+
// end::answer[]
29+
public static void main(String[] args) {
30+
new _0074_SearchA2DMatrix().searchMatrix(new int[][]{
31+
{1, 3, 5, 7},
32+
{10, 11, 16, 20},
33+
{23, 30, 34, 60}},
34+
3);
35+
}
36+
}

0 commit comments

Comments
 (0)