Lintcode186 Max Points on a Line solution 题解

【题目描述】

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

给出二维平面上的n个点,求最多有多少点在同一条直线上。

【题目链接】

www.lintcode.com/en/problem/max-points-on-a-line/

【题目解析】

(1)两点确定一条直线

(2)斜率相同的点落在一条直线上

(3)坐标相同的两个不同的点 算作2个点

使用Hashmap来存放键值对,Key用来存放斜率,value存放该斜率下的点的个数,对每个点进行循环遍历,计算该点和其余各个点的斜率,然后更新Hashmap中对应的值,每个点都计算出点数最大的值,保存在局部变量localmax中,维护一个全局变量max来存放最后的结果。

【参考答案】

www.jiuzhang.com/solutions/max-points-on-a-line/

你可能感兴趣的:(Lintcode186 Max Points on a Line solution 题解)