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>来存储具有相同斜率值的的点的数量,简洁高效。

 1 /*
 2     Definition for a point
 3 */
 4 // struct Point {
 5 //     int x;
 6 //     int y;
 7 //     Point():x(0),y(0) {}
 8 //     Point (int a, int b):x(0), y(0) {}
 9 // };
10
11 int maxPoints(vector<Point> &points) {
12     if (points.empty())
13         return 0;
14     if (points.size() <= 2)
15         return points.size();
16
17     int numPoints = points.size();
18     map<double, int> pmap;    //存储斜率-点数对应值
19     int numMaxPoints = 0;
20
21     for (int i = 0; i != numPoints - 1; ++i) {
22         int numSamePoints = 0, numVerPoints = 0;  //针对每个点分别做处理
23
24         pmap.clear();
25
26         for (int j = i + 1; j != numPoints; ++j) {
27             if(points[i].x != points[j].x) {
28                 double slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x);
29                 if (pmap.find(slope) != pmap.end())
30                     ++ pmap[slope];   //具有相同斜率值的点数累加
31                 else
32                     pmap[slope] = 1;
33             }
34             else if (points[i].y == points[j].y)
35                 numSamePoints ++;   //重合的点
36             else
37                 numVerPoints ++;    //垂直的点
38
39         }
40         map<double, int>::iterator it = pmap.begin();
41         for (; it != pmap.end(); ++ it) {
42             if (it->second > numVerPoints)
43                 numVerPoints = it->second;
44         }
45         if (numVerPoints + numSamePoints > numMaxPoints)
46             numMaxPoints = numVerPoints + numSamePoints;
47     }
48     return numMaxPoints + 1;
49 }
时间: 2024-08-03 09:27:49

LeetCode: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][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轴平行,

【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