//定义二维平面上的点
struct Point
{
int x;
int y;
Point(int a=, int b=):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+;
else
slope = double(P1.y-P2.y)/(P1.x-P2.x);
return slope;
} class Solution {
public:
int maxPoints(vector<Point> &points)
{
int max=;
for(vector<Point>::iterator it=points.begin(); it!=points.end(); ++it)
{
map<double, int> Line_count;
int same_count=; //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.的更多相关文章

  1. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  2. 【LeetCode OJ】Max Points on a Line

    Problem: Given n points on a 2D plane, find the maximum number of points that lie on the same straig ...

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

  4. 【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 ...

  5. 【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. ...

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

  7. leetcode 149. Max Points on a Line --------- java

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

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

  9. LeetCode之Max Points on a Line Total

    1.问题描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

随机推荐

  1. 【HDOJ】3419 The Three Groups

    记忆化搜索. /* 3419 */ #include <cstdio> #include <cstring> #include <cstdlib> #define ...

  2. Unity 随机数的使用

    脚本语言:C# 一个比较常用的例子是游戏中的主角碰到场景中的NPC时,NPC会随机做出反应,例如有50%几率来友好的致敬,25%几率走开,20%几率反身攻击和%%的几率赠送礼物. 创建一个NPCTes ...

  3. 今天修改bug基本完成

    今天修改bug基本完成 在修改bug过程中配到几个郁闷的问题,印象最深的两个1.检查出生日期或日期的合法性,引用的日历控件不能完全保证取到值时操作,现在突然想到或许我该仔细研究日历控件接口,在返回错误 ...

  4. C#:ref和out的联系及区别

    一:ref 关键字使参数按引用传递. 其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中.若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字. ...

  5. delphi record 内存怎么释放

    delphi record 内存怎么释放 是不需要释放的,除非你使用指针方式生成的.

  6. A Mini Locomotive(动态规划 01)

     /*  题意:选出3个连续的 数的个数  为K的区间,使他们的和最大 分析: dp[j][i]=max(dp[j-k][i-1]+value[j],dp[j-1][i]);   dp[j][i]:从 ...

  7. Clairewd’s message - HDU 4300(next[]运用)

    题目大意:给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能.   分析:题 ...

  8. centos下的mysql安装

    卸载mysql yum remove mysql mysql-server mysql-libs compat-mysql51 rm -rf /var/lib/mysql rm /etc/my.cnf ...

  9. java 基础(匿名内部类)

    匿名内部类 特点:不对外公开,进行实现功能,继承类,继承抽象类,实现某个接口的匿名内部类,实现相应的方法 特别注意:匿名内部类,匿名指的是 ,new 关键字右边的那个对象--如继承,或是接口    { ...

  10. Android GridView属性集合

    GridView的一些特殊属性: 1.android:numColumns=”auto_fit”   //GridView的列数设置为自动 2.android:columnWidth=”90dp &q ...