Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit954b2e7

Browse files
authored
Update _149.java
1 parent9c593b9 commit954b2e7

File tree

1 file changed

+23
-88
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-88
lines changed
Lines changed: 23 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,30 @@
1-
packagecom.fishercoder.solutions;
1+
**Updatedsolution** [credits](https://leetcode.com/problems/max-points-on-a-line/discuss/328269/A-Java-solution-with-my-understanding)
22

3-
importcom.fishercoder.common.classes.Point;
4-
importjava.util.HashMap;
5-
importjava.util.Map;
6-
7-
/**
8-
* 149. Max Points on a Line
9-
*
10-
* Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
11-
*
12-
* Example 1:
13-
* Input: [[1,1],[2,2],[3,3]]
14-
* Output: 3
15-
* Explanation:
16-
* ^
17-
* |
18-
* | o
19-
* | o
20-
* | o
21-
* +------------->
22-
* 0 1 2 3 4
23-
*
24-
* Example 2:
25-
* Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
26-
* Output: 4
27-
* Explanation:
28-
* ^
29-
* |
30-
* | o
31-
* | o o
32-
* | o
33-
* | o o
34-
* +------------------->
35-
* 0 1 2 3 4 5 6
36-
*
37-
*/
38-
publicclass_149 {
39-
40-
publicstaticclassSolution1 {
41-
/** credit: https://leetcode.com/problems/max-points-on-a-line/discuss/47113/A-java-solution-with-notes */
42-
publicintmaxPoints(Point[]points) {
43-
if (points ==null) {
44-
return0;
45-
}
46-
if (points.length <=2) {
47-
returnpoints.length;
48-
}
49-
50-
Map<Integer,Map<Integer,Integer>>map =newHashMap<>();
51-
intresult =0;
52-
for (inti =0;i <points.length;i++) {
53-
map.clear();
54-
intoverlap =0;
3+
classSolution {
4+
publicintmaxPoints(int[][]points) {
5+
if(points.length <3)returnpoints.length;
556
intmax =0;
56-
for (intj =i +1;j <points.length;j++) {
57-
intx =points[j].x -points[i].x;
58-
inty =points[j].y -points[i].y;
59-
if (x ==0 &&y ==0) {
60-
overlap++;
61-
continue;
62-
}
63-
intgcd =generateGCD(x,y);
64-
if (gcd !=0) {
65-
x /=gcd;
66-
y /=gcd;
67-
}
68-
69-
if (map.containsKey(x)) {
70-
if (map.get(x).containsKey(y)) {
71-
map.get(x).put(y,map.get(x).get(y) +1);
72-
}else {
73-
map.get(x).put(y,1);
7+
HashMap<Long,Integer>map =newHashMap<Long,Integer>();
8+
for(inti =0;i <points.length;i++) {
9+
intdup =1;
10+
map.clear();
11+
for(intj =i +1;j <points.length;j++) {
12+
intdx =points[j][0] -points[i][0],dy =points[j][1] -points[i][1];
13+
if(dx ==0 &&dy ==0)dup++;
14+
else {
15+
intgcd =getGcd(dx,dy);
16+
longslope = ((long)(dy /gcd) <<32) + (dx /gcd);
17+
map.put(slope,map.getOrDefault(slope,0) +1);
18+
}
7419
}
75-
}else {
76-
Map<Integer,Integer>m =newHashMap<>();
77-
m.put(y,1);
78-
map.put(x,m);
79-
}
80-
max =Math.max(max,map.get(x).get(y));
20+
max =Math.max(max,dup);
21+
for(Map.Entry<Long,Integer>entry :map.entrySet())
22+
max =Math.max(max,entry.getValue() +dup);
8123
}
82-
result =Math.max(result,max +overlap +1);
83-
}
84-
returnresult;
24+
returnmax;
8525
}
86-
87-
privateintgenerateGCD(inta,intb) {
88-
if (b ==0) {
89-
returna;
90-
}else {
91-
returngenerateGCD(b,a %b);
92-
}
26+
27+
intgetGcd(inta,intb) {
28+
returnb ==0 ?a :getGcd(b,a %b);
9329
}
94-
}
9530
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp