Skip to content

Commit 768b854

Browse files
committed
二刷240
1 parent 4b9903f commit 768b854

File tree

12 files changed

+74
-5
lines changed

12 files changed

+74
-5
lines changed

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
{leetcode}/problems/search-a-2d-matrix-ii/[LeetCode - Search a 2D Matrix II^]
55

6-
从右上角开始搜索,小则下移,大则左移,这个方案真是精妙!
7-
8-
image:images/0240-1.jpg[]
9-
106

117
Write an efficient algorithm that searches for a value in an _m_ x _n_ matrix. This matrix has the following properties:
128

@@ -35,10 +31,50 @@ Given target = `5`, return `true`.
3531
Given target = `20`, return `false`.
3632

3733

34+
== 思路分析
35+
36+
不能对真个矩阵进行二分查找!可以对单行做二分查找。
37+
38+
从右上角开始搜索,小则下移,大则左移,这个方案真是精妙!
39+
40+
image::images/0240-01.png[{image_attr}]
41+
42+
image::images/0240-02.png[{image_attr}]
43+
44+
image::images/0240-03.png[{image_attr}]
45+
46+
image::images/0240-04.png[{image_attr}]
47+
48+
image::images/0240-05.png[{image_attr}]
49+
50+
image::images/0240-06.png[{image_attr}]
3851

3952
[[src-0240]]
53+
[tabs]
54+
====
55+
一刷::
56+
+
57+
--
4058
[{java_src_attr}]
4159
----
4260
include::{sourcedir}/_0240_SearchA2DMatrixII.java[tag=answer]
4361
----
62+
--
63+
64+
二刷::
65+
+
66+
--
67+
[{java_src_attr}]
68+
----
69+
include::{sourcedir}/_0240_SearchA2DMatrixII_2.java[tag=answer]
70+
----
71+
--
72+
====
73+
74+
== 参考资料
75+
76+
. 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 - 官方题解^]
77+
. 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 - 贪心,清晰图解^]
78+
79+
4480

docs/images/0240-01.png

28.8 KB
Loading

docs/images/0240-02.png

45.2 KB
Loading

docs/images/0240-03.png

45.4 KB
Loading

docs/images/0240-04.png

46.2 KB
Loading

docs/images/0240-05.png

46.1 KB
Loading

docs/images/0240-06.png

46.2 KB
Loading

docs/images/0240-1.jpg

-4.5 KB
Binary file not shown.

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,11 @@
585585
|{doc_base_url}/0050-powx-n.adoc[题解]
586586
|分治,快速幂,迭代实现还需要再思考理解一下。
587587

588+
|{counter:codes}
589+
|{leetcode_base_url}/search-a-2d-matrix-ii/[240. Search a 2D Matrix II^]
590+
|{doc_base_url}/0240-search-a-2d-matrix-ii.adoc[题解]
591+
|不能对矩阵做二分查找,可以对单行做二分查找。要分析题目,查看如何排除不符合要求的元素。
592+
588593
|===
589594

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class _0050_PowXN_2 {
44
// tag::answer[]
55
/**
66
* @author D瓜哥 · https://www.diguage.com
7-
* @since 2020-01-13 21:19
7+
* @since 2024-09-15 17:13:46
88
*/
99
public double myPow(double x, long n) {
1010
if (x == 0) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class _0240_SearchA2DMatrixII {
4141
* Memory Usage: 50.3 MB, less than 5.66% of Java online submissions for Search a 2D Matrix II.
4242
*
4343
* Copy from: https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/66140/My-concise-O(m%2Bn)-Java-solution[(1) My concise O(m+n) Java solution - LeetCode Discuss]
44+
*
45+
* @author D瓜哥 · https://www.diguage.com
46+
* @since 2020-01-23 10:04
4447
*/
4548
public boolean searchMatrix(int[][] matrix, int target) {
4649
if (Objects.isNull(matrix) || matrix.length == 0) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0240_SearchA2DMatrixII_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2024-09-15 18:14:17
9+
*/
10+
public boolean searchMatrix(int[][] matrix, int target) {
11+
int row = 0, column = matrix[0].length - 1;
12+
while (row < matrix.length && 0 <= column) {
13+
int num = matrix[row][column];
14+
if (num == target) {
15+
return true;
16+
} else if (target < num) {
17+
column--;
18+
} else {
19+
row++;
20+
}
21+
}
22+
return false;
23+
}
24+
// end::answer[]
25+
}

0 commit comments

Comments
 (0)