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

示例 1:

输入: [[1,1],[2,2],[3,3]]
输出: 3
解释:
^
|
|        o
|     o
|  o  
+------------->
0  1  2  3 4

示例 2:

输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
输出: 4
解释:
^
|
| o
|     o   o
|      o
|  o   o
+------------------->
0  1  2  3  4  5  6 看到此题,第一想法是用一个数据结构来表示某一条直线,直线的表达式有y = ax + b ,那提取出 a和b是不是就可以了,写完发现还有x = 1这种直线。然后想通过hashmap来保存直线对应的点数,如果参数a和b是个1/3这种值,由于精度问题,算出来的两个直线的数据结构的hash值就不一样,hashmap就是认为是两个key。 最后无奈换成分数表达式 y = (a1/a2)*x + b1/b2; b1/b2 = (a2*y - a1*x)/a2;这样4个int变量,就可以精确表示一条直线.当然,分数需要进行约分!
struct FPoint {
int a1;
int a2;
int b1;
int b2;
FPoint() : a1(), a2(), b1(), b2() {}
FPoint(int _a1, int _a2)
{
if (_a1 * _a2 > )
{
a1 = abs(_a1);
a2 = abs(_a2);
}
else
{
a1 = - * abs(_a1);
a2 = abs(_a2);
}
b1 = ;
b2 = ;
}
bool Contain(Point p)const
{
long long int t1 = 1L, t2 = 1L;
t1 = t1 * p.y * (a2*b2);
t2 = t2 * a1*b2*p.x + b1*a2;
return a2 == ? p.x == b1 / b2 : t1 == t2;
}
void Normal()
{
if (a1 == && a2 == )
{
}
else if (a1 == )
a2 = ;
else if (a2 == )
a1 = ;
else
{
int s = a1*a2 > ? : -;
a1 = abs(a1);
a2 = abs(a2);
int _gcd = GCD(a1, a2);
a1 = s * a1 / _gcd;
a2 = a2 / _gcd; if (b1 == )
b2 = ;
else
{
s = b1*b2 > ? : -;
b1 = abs(b1);
b2 = abs(b2);
_gcd = GCD(b1, b2);
b1 = s * b1 / _gcd;
b2 = b2 / _gcd;
}
} }
  //最大公约数
int GCD(int a, int b){
int m = a, n = b, r;
if (m < n){
int temp = m;
m = n;
n = temp;
}
r = m % n;
while (r){
m = n;
n = r;
r = m % n;
}
return n;
}
};
struct RecordHash
{
size_t operator()(const FPoint& f) const{
return hash<int>()(f.a1) ^ hash<int>()(f.a2) ^ hash<int>()(f.b1) ^ hash<int>()(f.b2);
}
};
struct RecordCmp
{
bool operator()(const FPoint& f1, const FPoint& f2) const{
return f1.a1 == f2.a1 && f1.a2 == f2.a2 &&f1.b1 == f2.b1&&f1.b2 == f2.b2;
}
}; class Solution {
public:
FPoint GetPoint(Point a, Point b)
{
FPoint f(b.y - a.y, b.x - a.x );
if (f.a2 == )
{
f.b1 = a.x;
f.b2 = ;
}
else
{
f.b1 = (f.a2*a.y - f.a1*a.x);
f.b2 = f.a2;
}
return f;
}
int maxPoints(vector<Point>& points) {
if (points.size() <= )
{
return points.size();
}
unordered_set<int> index_set;
unordered_map<FPoint, int, RecordHash, RecordCmp> line_map;
int max_point = ;
for (int i = ; i < points.size(); i++)
{
unordered_map<FPoint, int, RecordHash, RecordCmp>::iterator itr = line_map.begin();
for (; itr != line_map.end(); itr++)
{
if (itr->first.Contain(points[i]))
{
itr->second++;
max_point = max(max_point, itr->second);
if (index_set.count(i) == )index_set.insert(i);
}
}
for (int r = ; r < points.size() ; r++)
{
if (r != i && index_set.count(r) == )
{
FPoint f = GetPoint(points[i], points[r]);
f.Normal();
if (line_map[f] == )
{
line_map[f]++;
}
if (index_set.count(i) == )
{
index_set.insert(i);
}
max_point = max(max_point, line_map[f]);
}
}
}
return max_point;
}
};

Max Points on a Line(直线上最多的点数)的更多相关文章

  1. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  2. Leetcode 149.直线上最多的点数

    直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o ...

  3. Java实现 LeetCode 149 直线上最多的点数

    149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...

  4. [Swift]LeetCode149. 直线上最多的点数 | 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. ...

  5. leetcode 149. 直线上最多的点数 解题报告

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o +------- ...

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

  7. [leetcode]Max Points on a Line @ Python

    原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...

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

  9. BZOJ3403: [Usaco2009 Open]Cow Line 直线上的牛

    3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 48  Solved: 41[S ...

随机推荐

  1. KMP算法题集

    模板 caioj 1177 KMP模板 #include<bits/stdc++.h> #define REP(i, a, b) for(register int i = (a); i & ...

  2. poj 2663 Tri Tiling (状压dp+多米诺骨牌问题+滚动数组反思)

    本来直接一波状压dpAC的 #include<cstdio> #include<cstring> #include<algorithm> #define REP(i ...

  3. 紫书 例题8-1 UVa 120(构造法)

    #include<cstdio> #include<iostream> #include<sstream> #include<algorithm> #d ...

  4. 紫书 习题7-14 UVa 307(暴搜+剪枝)

    这道题一开始我想的是在排序之后只在头和尾往中间靠近来找木块, 然后就WA, 事实证明这种方法是错误的. 然后参考了别人的博客.发现别人是直接暴搜, 但是加了很多剪枝, 所以不会超时. 我也想过这个做法 ...

  5. MySQL 数据备份与还原 转载

    MySQL 数据备份与还原 一.数据备份 1.使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldum ...

  6. mysql 全表扫描、全索引扫描、索引覆盖(覆盖索引)

    full index scan:全索引扫描,查询时,遍历索引树来获取数据行.如果数据不是密集的会产生随机IO 在执行计划中是Type列,index full table scan:通过读物理表获取数据 ...

  7. 基于ORA-12170 TNS 连接超时解决办法详解

    转自原文 基于ORA-12170 TNS 连接超时解决办法详解 1.开始----程序-----Oracle------配置和移植工具-----Net Manager----本地----服务命名---o ...

  8. 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

    [145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  9. HBase基本数据操作具体解释

    引言 本文档參考最新(截止2014年7月16日)的官方Ref Guide.Developer API编写. 全部代码均基于"hbase 0.96.2-hadoop2"版本号编写.均 ...

  10. 千万别相信鲁大师的硬件測温柔CPU測温功能!!

    非常多人本来随手安装的一个软件. 相信也信任得过它 . 这下让我測试对它失望了.没想到鲁大师这个測温功能实在太搓了!! 白白浪费了我一晚上,  搞来了硅胶 ,弄了几遍  , 还是一样, 还以为买了水货 ...