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

思路

关键是浮点数做key不靠谱,struct hash以及 int calcGCD(int a, int b)的写法

代码

/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
struct Key{
int first;
int second;
Key(int f, int s) : first(f), second(s){};
bool operator==(const Key &other) const{
return first == other.first
&& second == other.second;
}
};
namespace std {
template <>
struct hash<Key>{
size_t operator()(const Key& k) const{
// Compute individual hash values for first,
// second and third and combine them using XOR
// and bit shifting:
return hash<int>()(k.first)
^ (hash<int>()(k.second) << 1);
}
};
}
class Solution {
private:
int calcGCD(int a, int b) {
if (b == 0) //end (divisible, it is the gcd)
return a;
return calcGCD(b, a % b);
}
Key norm(int a, int b) {
int gcd = calcGCD(a, b);
if(gcd == 0) return Key(0,0);//同一个点
return Key(a/gcd, b/gcd);
}
public:
int maxPoints(vector<Point> &points) {
if(points.empty()) return 0;//不然会Input: []; Output: 1
unordered_map<Key, int> nSamelines;
int maxSamelines = 0;
//每次fix一个点,看其他点和它的共线情况
for(int i = 0; i < points.size(); i++){
nSamelines.clear();
nSamelines.emplace(Key(0,0), 1);
for(int j = i+1; j < points.size(); j++){
Key slope = norm(points[i].y-points[j].y, points[i].x-points[j].x);
//if(slope == Key(0,0)) continue;//得看题意了Input:[(0,0),(0,0)] Output:1 Expected:2
auto it = nSamelines.find(slope);
if(it != nSamelines.end()){
it->second += 1;
} else {
nSamelines.emplace(slope, 1);
}
}
if(maxSamelines < nSamelines[Key(0,0)])
maxSamelines = nSamelines[Key(0,0)];
for(auto entry : nSamelines){
if(!(entry.first == Key(0,0)) && (maxSamelines < entry.second + nSamelines[Key(0,0)])) {
maxSamelines = entry.second + nSamelines[Key(0,0)];
}
}
}
return maxSamelines;
}
};

Max Points on a Line的更多相关文章

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

  2. [LeetCode OJ] Max Points on a Line

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

  3. [LintCode] 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】149. 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 on the ...

  5. 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 on the ...

  6. [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. Max Points on a Line leetcode java

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  8. 【Max Points on a Line 】cpp

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  9. 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 lin ...

  10. [LeetCode] 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. 对COM 组件的调用返回了错误 HRESULT E_FAIL

    .net ppt转pdf时报以下错误: 对COM 组件的调用返回了错误 HRESULT E_FAIL 在服务器端打开PPT,选项--另存为--PDF,发现PowerPoint报了个错误: “无法找到打 ...

  2. x64内联汇编注意点

    #include <windows.h> #include <stdio.h> extern "C" int MyPrintf(ULONGLONG,ULON ...

  3. 伸缩盒子模型,旧的伸缩盒子模型。浏览器内核、css继承属性

  4. [转载]什么是FCKeditor?功能强大的HTML编辑器!

    天天在用FCKeditor写博客,但一直不清楚FCKeditor到底是什么,今天终于找到了一些相关的资料,大家一起来分享下. FCKeditor文本编辑程序(共享软件)为用户提供在线的文档编辑服务,其 ...

  5. Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)

    http://codeforces.com/contest/737 A: 题目大意: 有n辆车,每辆车有一个价钱ci和油箱容量vi.在x轴上,起点为0,终点为s,中途有k个加油站,坐标分别是pi,到每 ...

  6. java 字符串转成 json 数组并且遍历

    当需要把一串字符串转成一个json 数组 ,并遍历其中的内容时. 首先要导入 net.sf.json.JSONArray和net.sf.json.JSONObject 两个jar 包 String s ...

  7. 阿里云服务器被挖矿minerd入侵的解决办法

    上周末,更新易云盘的时候,发现阿里云服务器CPU很高,执行 top 一看,有个进程minerd尽然占用了90%多的CPU, 赶紧百度一下,查到几篇文章都有人遇到同样问题 Hu_Wen遇到的和我最相似, ...

  8. 在PHP中如何实现在做了么个操作后返回到指定页面

    我们经常会碰到类似用户在没有登录的情况下进行提问.评论,需要用户登录后返回刚才浏览的网页,这种功能用cookie保存当前url地址来实现.我用的是jquery,读者需要懂点jquery中的ajax请求 ...

  9. Intellij IDEA 创建Web项目并在Tomcat中部署运行(不使用maven)【转载】

    原文链接:http://www.thinksaas.cn/topics/0/350/350000.html 一.创建Web项目 1.File -> New Module,进入创建项目窗口 2.选 ...

  10. java学习笔记(菜鸟原创)

    搭建Java开发环境使用开发工具开发Myeclipse基础核心:JAVASEEEME面向对象 API JVM.JAVAEE是指java enterprise edition,java企业版,多用于企业 ...