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. qt-creator astyle Peizhi

    --style=ansi -s4 --pad-oper -S -N -L -m0 --add-brackets -M40 --suffix=none --convert-tabs %{CurrentD ...

  2. 通过JDBK操作数据库

    一.配置程序--让我们程序能找到数据库的驱动jar包1.把.jar文件复制到项目中去,整合的时候方便.2.在eclipse项目右击"构建路径"--"配置构建路径" ...

  3. OpenCV 学习

    #include <opencv2\opencv.hpp> #include <iostream> #include <opencv2\highgui\highgui.h ...

  4. 【转】responseText,responseBody,responseXML差别

    为了做ajax的代理,研究了下服务器端的xmlhttp和客户端ajax中的xmlhttp,做了个比较 由于我一直使用JavaScript作为Asp的教本语言,所以比较起来更清楚.服务器端的xmlhtt ...

  5. 常见JDBC连接数据库字符串

    1.Mysql 驱动类:com.mysql.jdbc.Driver 连接字符串:jdbc:mysql://localhost:3306/dbname 2.Oracle 驱动类:oracle.jdbc. ...

  6. (转发)centos,redhat 系统为php安装memcached扩展

    转自:http://www.itnose.net/detail/6111623.html 1. 通过yum安装 yum -y install memcached #安装完成后执行: memcached ...

  7. RHEL6 --部署phpMyAdmin与论坛系统

    一.rpm安装LAMP平台部署phpMyAdmin 二.搭建wordpress个人博客系统 三.搭建论坛系统 一.rpm安装LAMP平台及部署phpMyAdmin 1.phpMyAdmin简介及获取方 ...

  8. Cordova+ionic 开发hybird App --- 开发环境搭建

    Cordova 开发hybird App 开发环境搭建 一.一些基础概念: Ant : 简单说来可以这么理解,如果你用记事本写JAVA程序,然后在cmd里输入javac命令编译它,但是有一天你发现每次 ...

  9. ubuntu 安装MTK 移动终端usb驱动

    lsusbBus 001 Device 002: ID 8087:8000 Intel Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation ...

  10. Hadoop:输入,输出,key,value格式

    map: (K1, V1) → list(K2, V2) reduce: (K2, list(V2)) → list(K3, V3) (K1, V1): jobConf.setInputKeyClas ...