File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
solution/0149.Max Points on a Line Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int maxPoints (Point [] points ) {
3+ if ( points .length <= 2 ) return points .length ;
4+ int max = 2 ;
5+ for ( int i = 0 ; i < points .length ; i ++ ){
6+ int samePosition = 0 ;
7+ int sameSlope = 1 ;
8+ for ( int j = i + 1 ; j < points .length ; j ++ ){
9+ long x1 = points [j ].x - points [i ].x ;
10+ long y1 = points [j ].y - points [i ].y ;
11+ if ( x1 == 0 && y1 == 0 ){
12+ samePosition ++;
13+ } else {
14+ sameSlope ++;
15+ for (int k = j + 1 ; k < points .length ; k ++ ){
16+ long x2 = points [k ].x - points [i ].x ;
17+ long y2 = points [k ].y - points [i ].y ;
18+ if ( x1 * y2 == x2 * y1 ) sameSlope ++;
19+ }
20+ }
21+ if (max < (samePosition + sameSlope )) max = samePosition + sameSlope ;
22+ sameSlope = 1 ;
23+ }
24+ }
25+ return max ;
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments