[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& P1, const Point& P2)
{
double slope;
if(P1.x==P2.x)
slope = 1e+6;
else
slope = double(P1.y-P2.y)/(P1.x-P2.x);
return slope;
}

class Solution {
public:
int maxPoints(vector<Point> &points)
{
int max=0;
for(vector<Point>::iterator it=points.begin(); it!=points.end(); ++it)
{
map<double, int> Line_count;
int same_count=0; //same_count表示同一个点重复出现的个数

for(vector<Point>::iterator it2=it; it2!=points.end(); ++it2) //此处it2=points.begin()还是it2=it虽然不影响最终结果,但是对于时间复杂度影响很大,it2=it的时间复杂度要低很多
{
if(*it==*it2)
{
++same_count;
continue;
}
double slope = line_equation(*it, *it2);
++Line_count[slope];
}
set<int> iset;
for(map<double, int>::iterator iter = Line_count.begin(); iter!=Line_count.end(); ++iter)
iset.insert(iter->second);
int max_now = same_count;
if(iset.begin()!=iset.end())
max_now = same_count + *(--iset.end());
if(max_now>max)
max = max_now;
}
return max;
}
};

问题描述:在一个二维平面上有n个点,求出现在同一直线上的点的最大个数

分析:对每一个点计算与其他点(不包括与该点相同的点)连接成的直线的斜率,斜率重复出现的最大次数加成该点重复出现的个数,即为该点所在直线上拥有的最大点个数(比如该点既出现在直线L1上,又出现在直线L2上,直线L1上有这n个点中的3个点,直线L2上有这n个点中的5个点,我们得到的该点所处直线的拥有最多点的那一条直线上的点个数便是5,有点绕口~~)。

对每一个点进行相同的操作,便可得到n个点中出现在同一直线的点的最大个数。

只需考虑斜率就可以解决这个问题,之前考虑还需要截距,其实完全不必要,不要把问题想得太复杂~

[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.

时间: 2024-12-21 12:47:10

[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.的相关文章

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

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()

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

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: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][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

Microsoft - Find the K closest points to the origin in a 2D plane

Find the K closest points to the origin in a 2D plane, given an array containing N points. 用 max heap 做 /* public class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } */ public List<Point> findKClosest(P