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.
思路
关键是浮点数做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的更多相关文章
- 【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 OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- [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. ...
- 【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 ...
- 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 ...
- [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. ...
- 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 ...
- 【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 ...
- 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 ...
- [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. ...
随机推荐
- swift-分支语句
// switch的基本用法 // 1>switch后面的()可以省略 // 2>case中语句结束后不需要跟break // 3>在case中定义局部变量不需要跟{} // 4&g ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- JAVA中最常用的十个快捷键
http://blog.sina.com.cn/s/blog_5fb39f910101dc2b.html 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以 ...
- Android编程思想双11口诀
能复制就复制,节约时间避免出错 保留原本结构,简单上手容易调试 说明随手可得,不用上网或打开文档 增加必要注释,说明功能和使用方法 命名尽量规范,容易查找一看就懂 函数尽量嵌套,减少代码容易修改 最先 ...
- VS2012+LUA环境搭建
1 .启动VS2012,选择C++下的"win32"项目类型中的"Win2控制台应用程序" 2.工具——选项——项目和解决方案——VC++目录——可执行程序(C ...
- JSP内置对象---out内置对象
<%@ page language="java" import="java.util.*" contentType="text/html; ch ...
- javaIO系统----再看装饰者模式
javaIO系统拥有各种各样的类,尤其是每次要进行读写操作时,总会一层套一层的new,以前不明白为什么要这样做,不过学习了适配器模式后,对于这种做法立刻了解了:动态扩展IO的功能,使之符合使用者的习惯 ...
- druid.properties的配置
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://NoOne:3306/eyes<!--需修改--> username=root ...
- 0x7c95caa2指令引用的0x00000000内存 该内存不能read
出现这样的错误,往往和动态库有关系! 解决方法:
- Power of Four(Difficulty: Easy)
题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...