//定义二维平面上的点
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. mt7601 driver

    http://download.csdn.net/detail/u011500307/7011649 http://my.oschina.net/fgq611/blog/180750 http://b ...

  2. 【转】color颜色十六进制编码大全

    原文网址:http://blog.csdn.net/coder_pig/article/details/18262105 最近刚开始学web,发现好的颜色搭配可以让自己的网页更加美观, 中午不想做事, ...

  3. (转载)mysql 用drop和delete方法删除用户的区别

    (转载)http://hi.baidu.com/yymagento/item/56c3f6184bce8347e75e06db 在学习drop方法删除用户时,按照书上讲的一直没操作成功,后来到网上查了 ...

  4. 《University Calculus》-chaper13-多重积分-三重积分的引入

    承接之前对一重积分和二重积分的介绍,这里我们自然的引出三重积分. 在二重积分的引入中,我们曾经埋下过一个小伏笔,二重积分的几何意义是求解一个体积,但是我们仅仅限定在了曲顶柱体的几何体,那么对于完全由曲 ...

  5. Flex3在应用RemoteObject出现问题解决方法

    出现该问题 <mx:RemoteObject id="robj" destination="hello" endpoint="http://lo ...

  6. Azkaban2官方配置文档

    最近工作实在是太忙了,我把之前翻译的官方的文档先放上来吧,希望对大家有所帮助~ 介绍 Azkaban2新功能: 1.Web UI 2.简单工作流上传 3.更容易设置job的依赖关系 4.调度工作流 5 ...

  7. angularJS $watch $digest $apply

    一 简介AngularJS提供了一个非常酷的特性叫做双向数据绑定(Two-way Data Binding),这个特性大大简化了我们的代码编写方式.数据绑定意味着当View中有任何数据发生了变化,那么 ...

  8. 两种动态载入修改后的python模块的方法

    方案一:循环导入/删除模块 a.py import sys, time while True: from b import test test() del sys.modules(b) time.sl ...

  9. setState的同步更新

    react中的setState特点: 是异步操作函数: 组件在还没有渲染之前, this.setState 还没有被调用: 批量执行 State 转变时让 DOM 渲染更快(相对比一个一个的setSt ...

  10. SetupFactory +添加frm2.0工具

    网盘链接: http://pan.baidu.com/s/1c1DFRJM 带单独的添加frm2.0工具 原setupfactory文件下载地址:http://www.pc0359.cn/downin ...