Skip to content

Commit 99ff0c1

Browse files
committed
一刷149
1 parent 4af622d commit 99ff0c1

File tree

7 files changed

+109
-41
lines changed

7 files changed

+109
-41
lines changed

README.adoc

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

1072-
//|{counter:codes}
1073-
//|{leetcode_base_url}/max-points-on-a-line/[149. Max Points on a Line^]
1074-
//|{source_base_url}/_0149_MaxPointsOnALine.java[Java]
1075-
//|{doc_base_url}/0149-max-points-on-a-line.adoc[题解]
1076-
//|Hard
1077-
//|
1072+
|{counter:codes}
1073+
|{leetcode_base_url}/max-points-on-a-line/[149. Max Points on a Line^]
1074+
|{source_base_url}/_0149_MaxPointsOnALine.java[Java]
1075+
|{doc_base_url}/0149-max-points-on-a-line.adoc[题解]
1076+
|Hard
1077+
|
10781078

10791079
|{counter:codes}
10801080
|{leetcode_base_url}/evaluate-reverse-polish-notation/[150. Evaluate Reverse Polish Notation^]

docs/0149-max-points-on-a-line.adoc

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,78 @@
11
[#0149-max-points-on-a-line]
2-
= 149. Max Points on a Line
2+
= 149. 直线上最多的点数
33

4-
{leetcode}/problems/max-points-on-a-line/[LeetCode - Max Points on a Line^]
54

6-
Given _n_ points on a 2D plane, find the maximum number of points that lie on the same straight line.
5+
https://leetcode.cn/problems/max-points-on-a-line/[LeetCode - 149. 直线上最多的点数 ^]
76

8-
*Example 1:*
7+
给你一个数组 `points` ,其中 `points[i] = [x~i~, y~i~]` 表示 *X-Y* 平面上的一个点。求最多有多少个点在同一条直线上。
98

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
*Input:* [[1,1],[2,2],[3,3]]
13-
*Output:* 3
14-
*Explanation:*
15-
^
16-
|
17-
| o
18-
| o
19-
| o
20-
+------------->
21-
0 1 2 3 4
22-
----
9+
*示例 1:*
2310

24-
*Example 2:*
11+
image::images/0149-01.jpg[{image_attr}]
2512

26-
[subs="verbatim,quotes,macros"]
27-
----
28-
*Input:* [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
29-
*Output:* 4
30-
*Explanation:*
31-
^
32-
|
33-
| o
34-
| o o
35-
| o
36-
| o o
37-
+------------------->
38-
0 1 2 3 4 5 6
39-
----
13+
....
14+
输入:points = [[1,1],[2,2],[3,3]]
15+
输出:3
16+
....
17+
18+
*示例 2:*
19+
20+
image::images/0149-02.jpg[{image_attr}]
21+
22+
....
23+
输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
24+
输出:4
25+
....
26+
27+
*提示:*
28+
29+
* `+1 <= points.length <= 300+`
30+
* `points[i].length == 2`
31+
* `-10^4^ \<= x~i~, y~i~ \<= 10^4^`
32+
* `points` 中的所有点 *互不相同*
4033
41-
*NOTE:* input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
4234
4335
36+
== 思路分析
37+
38+
先确定两个点,然后再选一个点,计算三个点的斜率来查看是否在同一条直线。
39+
40+
[stem]
41+
++++
42+
(y_1 - y_2) / (x_1 - x_2) = (y_2 - y_3)/(x_2 - x_3)
43+
++++
44+
45+
等价于
46+
47+
[stem]
48+
++++
49+
(x_2 - x_3)* (y_1 - y_2) = (x_1 - x_2) * (y_2 - y_3)
50+
++++
51+
52+
这样就避免计算除法。
4453

4554
[[src-0149]]
55+
[tabs]
56+
====
57+
一刷::
58+
+
59+
--
4660
[{java_src_attr}]
4761
----
4862
include::{sourcedir}/_0149_MaxPointsOnALine.java[tag=answer]
4963
----
64+
--
65+
66+
// 二刷::
67+
// +
68+
// --
69+
// [{java_src_attr}]
70+
// ----
71+
// include::{sourcedir}/_0149_MaxPointsOnALine_2.java[tag=answer]
72+
// ----
73+
// --
74+
====
75+
76+
== 参考资料
77+
5078

docs/images/0149-01.jpg

10.1 KB
Loading

docs/images/0149-02.jpg

11.6 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ include::0146-lru-cache.adoc[leveloffset=+1]
381381

382382
include::0148-sort-list.adoc[leveloffset=+1]
383383

384-
// include::0149-max-points-on-a-line.adoc[leveloffset=+1]
384+
include::0149-max-points-on-a-line.adoc[leveloffset=+1]
385385

386386
include::0150-evaluate-reverse-polish-notation.adoc[leveloffset=+1]
387387

logbook/202503.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,12 @@ endif::[]
931931
|{counter:codes2503}
932932
|{leetcode_base_url}/candy/[135. 分发糖果^]
933933
|{doc_base_url}/0135-candy.adoc[题解]
934-
|⭕️初步设想是一次遍历。后来看题解,可以从两端遍历,再就和。这样可以省去很多判断。
934+
|⭕️ 初步设想是一次遍历。后来看题解,可以从两端遍历,再就和。这样可以省去很多判断。
935+
936+
|{counter:codes2503}
937+
|{leetcode_base_url}/max-points-on-a-line/[149. 直线上最多的点数^]
938+
|{doc_base_url}/0149-max-points-on-a-line.adoc[题解]
939+
|⭕️ 通过计算 stem:[(y_1 - y_2) / (x_1 - x_2) = (y_2 - y_3)/(x_2 - x_3)] 即可确定三个点是否在同一个直线上。
935940

936941
|===
937942

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0149_MaxPointsOnALine {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-06-07 21:55:30
8+
*/
9+
public int maxPoints(int[][] points) {
10+
int length = points.length;
11+
int result = 1;
12+
for (int i = 0; i < length; i++) {
13+
int[] a = points[i];
14+
for (int j = i + 1; j < length; j++) {
15+
int[] b = points[j];
16+
int cnt = 2;
17+
for (int k = j + 1; k < length; k++) {
18+
int[] c = points[k];
19+
int m1 = (b[0] - c[0]) * (a[1] - b[1]);
20+
int m2 = (a[0] - b[0]) * (b[1] - c[1]);
21+
if (m1 == m2) {
22+
cnt++;
23+
}
24+
}
25+
result = Math.max(result, cnt);
26+
}
27+
}
28+
return result;
29+
}
30+
// end::answer[]
31+
public static void main(String[] args) {
32+
new _0149_MaxPointsOnALine()
33+
.maxPoints(new int[][]{{3, 3}, {1, 4}, {1, 1}, {2, 1}, {2, 2}});
34+
}
35+
}

0 commit comments

Comments
 (0)