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. spring开发的总结

    1,当出现无法创建bean,( Error creating bean with name 'fileUploadService': Injection of resource dependencie ...

  2. 【代码】二进制转BCD [转]

    BCD:Binary Coded Decimal 即用4位二进制编码表示1位的十进制数.   定义:BCD码这种编码形式利用了四个位元来储存一个十进制的数码,使二进制和十进制之间的转换得以快捷的进行. ...

  3. 【DIY】【外壳】木板 & 亚克力 加工

    —————————————————————————————————————————————————————————————————————— 一.途径 淘宝 https://item.taobao.c ...

  4. iOS_线程和进程的区别与联系

    首先是线程和进程的联系: 线程和进程都是由操作系统所负责的程序运行的基本单元,系统利用该基本单元实现对应用的并发性. 接下来就是线程和进程的区别: 线程和进程最大的区别就是它们是操作系统的两种资源管理 ...

  5. C++中的数组

    数组名作为参数时,传递的是数组的首地址, 主调函数中实参数组元素个数不应该少于形参数组的元素个数 把数组名作为参数时,一般不指定数组第一维的大小 即使指定,编译时也会被忽略的.

  6. javap反编译解释外部类直接使用内部类private字段的原理

    2016-07-04 15:56:39 我们都知道: 1.内部类可以直接访问外部类的private字段和方法: 2.非静态内部类持有外部类的引用: 3.外部类可以直接访问内部类的private字段和方 ...

  7. Coding源码学习第三部分(EaseStartView.m)

    首先接上篇的要做一个NSEnumerator 类的延展阅读. 枚举(NSEnumerator) (1)依附于集合类(NSArray,NSSet,NSDictionary),没有用来创建实例的接口. ( ...

  8. PDA项目介绍

    开发工具:Microsoft Visual Studio 2008 SDK:      Windows Mobile 6 SDK 数据库:     Oracle 开发语言:C#(3.5) 版本控制工具 ...

  9. jQuery LigerUI V1.2.2 (包括API和全部源码) 发布

    前言 这次版本主要对树进行了加载性能上面的优化,并解决了部分兼容性的问题,添加了几个功能点. 欢迎使用反馈. 相关链接 API:         http://api.ligerui.com/ 演示地 ...

  10. 《Pro Express.js》学习笔记——概述

    要学Node.js,先学Express.js. Express.js是Node.js官方推荐的基础框架. Express.js框架经过一系列的发展,已经到了4.x版本.新的版本解决了3.x之前版本的依 ...