Max Points on a Line(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。
示例 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(直线上最多的点数)的更多相关文章
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
- Leetcode 149.直线上最多的点数
直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o ...
- Java实现 LeetCode 149 直线上最多的点数
149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...
- [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. ...
- leetcode 149. 直线上最多的点数 解题报告
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o +------- ...
- 【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 ...
- [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 ...
- 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 ...
- BZOJ3403: [Usaco2009 Open]Cow Line 直线上的牛
3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 48 Solved: 41[S ...
随机推荐
- JS中的NaN
什么是NaN?它的类型是什么?如何可靠地测试一个值是否等于NaN? NaN属性表示“不是数字”的值.这个特殊值是由于一个操作数是非数字的(例如“abc”/ 4)或者因为操作的结果是非数字而无法执行的. ...
- Linux下Makefile的automake生成全攻略
作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的M ...
- ValidForm的使用
1.引入css 请查看下载文件中的style.css,把里面Validform必须部分拷贝到你的css中(文件中这个凝视 "/*==========下面部分是Validform必须的==== ...
- Spring中@Transactional事务回滚(含实例具体解说,附源代码)
一.使用场景举例 在了解@Transactional怎么用之前我们必须要先知道@Transactional有什么用. 以下举个栗子:比方一个部门里面有非常多成员,这两者分别保存在部门表和成员表里面,在 ...
- pat(A) 1066. Root of AVL Tree
代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> # ...
- node-webkit 主页面和 iframe 页通讯
<html lang="en-US"> <head> <title>Hello World!</title> <style&g ...
- Android解决ScrollView视图导致其底部的布局栏被推到上边的问题
近期有个xml布局文件,我说下大概意思: <ScrollView> ...... </ScrollView> <RelativeLayout> ...... < ...
- nyoj--214--单调递增子序列(二)(二分查找+LIS)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序 ...
- Mysql实战45讲 05讲深入浅出索引(下)极客时间 读书笔记
极客时间 Mysql实战45讲 04讲深入浅出索引(下)极客时间 笔记体会: 回表:回到主键索引树搜索的过程,称为回表覆盖索引:某索引已经覆盖了查询需求,称为覆盖索引,例如:select ID fro ...
- BOOL的getter方法
在代码中经常会看到这样的属性声明 @property (nonatomic,assign,getter = isRead)BOOL read; 这行代码的意思就是,声明一个BOOL类型的read,但是 ...