[LeetCode][JavaScript]Max Points on a Line

Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.



求有多少点在一直线上。

粗暴地用二重循环遍历。

每一轮都构造一个哈希表,用来记录斜率,斜率k = (y1 - y2) / (x1 - x2)。

注意到特殊情况:

1.两点重合,用countSamePoint记下重复的点,最后加到结果上。

2.两点与X轴平行,此时y1 - y2 = 0, k = 0,构造哈希表,提前塞一个slopeMap[0] = 0。

3.两点与Y轴平行,此时x1 - x2 = 0, k = Infinity,同上塞一个slopeMap[Infinity] = 0。

 1 /**
 2  * Definition for a point.
 3  * function Point(x, y) {
 4  *     this.x = x;
 5  *     this.x = y;
 6  * }
 7  */
 8 /**
 9  * @param {Point[]} points
10  * @return {number}
11  */
12 var maxPoints = function(points) {
13     if(points.length <= 1){
14         return points.length;
15     }
16     var res = -1, countSamePoint, max, i, j, slopeMap, curr, slope, tmp;
17     for(i = 0; i < points.length; i++){
18         curr = points[i];
19         max = 0;
20         countSamePoint = 1;
21         slopeMap = {};
22         slopeMap[0] = 0; slopeMap[Infinity] = 0;
23         for(j = 0; j < points.length; j++){
24             if(i === j){
25                 continue;
26             }
27             if(points[j].x === curr.x && points[j].y === curr.y){
28                 countSamePoint++;
29             }else{
30                 slope = (points[j].y - curr.y) / (points[j].x - curr.x);
31                 if(slopeMap[slope] === undefined){
32                     slopeMap[slope] = 1;
33                 }else{
34                     slopeMap[slope]++;
35                 }
36                 tmp = slopeMap[slope];
37                 if(tmp > max){
38                     max = tmp;
39                 }
40             }
41             if(max + countSamePoint > res){
42                 res = max + countSamePoint;
43             }
44         }
45     }
46     return res;
47 };

Test Cases:

 1 function test(){
 2     console.log(maxPoints([])); //0
 3     console.log(maxPoints([new Point(0,0)])); //1
 4     console.log(maxPoints([new Point(0,0),new Point(0,0)])); //2
 5     console.log(maxPoints([new Point(1,1),new Point(2,1)])); //2
 6     console.log(maxPoints([new Point(1,1),new Point(1,2)])); //2
 7     console.log(maxPoints([new Point(0,0),new Point(1,1),new Point(0,0)])); //3
 8     console.log(maxPoints([new Point(1,1),new Point(2,2),new Point(2,3),new Point(3,3),new Point(5,10)]));  //3
 9     console.log(maxPoints([new Point(1,1),new Point(1,1),new Point(2,2),new Point(2,2)])); //4
10
11 };
时间: 2024-12-24 00:50:28

[LeetCode][JavaScript]Max Points on a Line的相关文章

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma

[LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&

LeetCode OJ - Max Points on a Line

题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路: 第一反应:枚举两个点组成的直线,然后看其他的点在不在这条直线上,在此过程中统计最大值.此思路的复杂度 O(n^3) 参考了网上的思路:枚举第一个点,用unordered_map来记录其余的点和这个点的斜率,若斜率相同则代表这些点在同一直线上.避免double问题,把斜率转化成化简

LeetCode:Max Points on a line

题目:Max Points on a line Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 这道题需要稍微转变一下思路,用斜率来实现,试想找在同一条直线上的点,怎么判断在一条直线上,唯一的方式也只有斜率可以完成,我开始没想到,后来看网友的思路才想到的,下面是简单的实现:其中有一点小技巧,利用map<double, int>来存储具有相同斜率

【LeetCode】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. [题意] 求二维平面上n个点中,最多共线的点数. [思路] 比较直观的方法是,三层循环,以任意两点划线,判断第三个点是否在这条直线上. [Java代码] /** * Definition for a point. * class Point { * int x; * int y; * Point()

Java for LeetCode 149 Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路: 本题主要需要考虑到斜线的情况,可以分别计算出过points[i]直线最多含几个点,然后算出最大即可,由于计算points[i]的时候,前面的点都计算过了,所以不需要把前面的点考虑进去,所以问题可以转化为过points[i]的直线最大点的个数,解题思路是用一个HashMap储存斜率,遍历p

[Leetcode][JAVA] Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 对每个点,考察其他点与它组成的直线斜率,使用HashMap将斜率与点个数对应起来. 需要注意的一点是特殊斜率,比如两点重复,两点横坐标相同.所以用String表示斜率比较好 获取斜率函数: 1 public String getSlope(Point p1, Point p2) 2 { 3 if(p

【leetcode】Max Points on a Line(hard)☆

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 思路: 自己脑子当机了,总是想着斜率和截距都要相同.但实际上三个点是一条直线的话只要它们的斜率相同就可以了,因为用了相同的参照点,截距一定是相同的. 大神的做法: 对每一个点a, 找出所有其他点跟a的连线斜率,相同为同一条线,记录下通过a的点的线上最大的点数. 找出每一个点的最大连线通过的点数. 其

leetcode[149]Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solutio