给定一个二维平面,平面上有 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
#include<bits/stdc++.h>

using namespace std;
struct Point {
int x;
int y;
Point() : x(0), y(0) {}
Point(int a, int b) : x(a), y(b) {}
}; bool operator<(const Point &a, const Point &b) {
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
} bool operator==(const Point &a, const Point &b) {
return a.x == b.x and a.y == b.y;
} int gcd(int a, int b) {
return b?gcd(b, a%b):a;
} struct pair_hash {
template <class T1, class T2>
std::size_t operator () (const std::pair<T1,T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
return h1 ^ h2;
}
}; const pair<int, int> zero_pair = make_pair(0, 0); pair<int,int> cmp(const Point &a, const Point &b) {
int c = b.y - a.y;
int d = b.x - a.x;
if(c == d && c == 0)
return make_pair(0,0);
if(c == 0)
return make_pair(0, 1);
if(d == 0)
return make_pair(1, 0);
int g = gcd(c, d);
return make_pair(c/g, d/g);
} class Solution {
public:
int maxPoints(vector<Point>& points) {
if(points.size() < 2)
return points.size();
sort(points.begin(), points.end());
unordered_map<pair<int,int>,int, pair_hash>mp;
int ret = 2, sz = points.size();
for(int i = 0; i < sz; ++i) {
mp.clear();
for(int j = i + 1; j < sz; ++j) {
auto xy = cmp(points[i], points[j]);
if(mp.count(xy)) {
mp[xy] += 1;
} else
mp[xy] = 2; if(mp.count(zero_pair) and xy != zero_pair) {
ret = max(ret, mp[xy] + mp[zero_pair] - 1);
} else
ret = max(ret, mp[xy]);
} }
return ret;
}
}; int main() { return 0;
}

leetcode 149. 直线上最多的点数 解题报告的更多相关文章

  1. Java实现 LeetCode 149 直线上最多的点数

    149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...

  2. Leetcode 149.直线上最多的点数

    直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o ...

  3. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  4. [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. ...

  5. Max Points on a Line(直线上最多的点数)

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o |     o | ...

  6. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  7. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  8. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

随机推荐

  1. [Python]面向对象近期笔记-super

    Python面向对象高级 直接调用父类方法 class A: def __init__(self): print("hello") class B(A): def __init__ ...

  2. JS获取页面传过来的值

    利用JS获取页面的传值,此方法只适应Get传值. 获取页面之间的传值,在后台我们很容易获取,那我们在前台只利用JS怎么写呢? 在看代码之前你需要了解的 ① 参考:W3C Location 对象 Loc ...

  3. 使用JavaScript动态的绑定、解绑 a 标签的onclick事件,防止重复点击

    页面上的 a 标签如下: <a class="more" style="cursor: pointer;" id="commentMore&qu ...

  4. hdu_5288_OO’s Sequence

    OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) ...

  5. 基于mybatis设计简单信息管理系统1

    驼峰式命名法 骆驼式命名法就是当变量名或函数名是由一个或多个单词连结在一起,而构成的唯一识别字时,第一个单词以小写字母开始:第二个单词的首字母大写或每一个单词的首字母都采用大写字母,例如:myFirs ...

  6. Spring Cloud 入门Eureka -Consumer服务消费(一)

    这里介绍:LoadBalancerClient接口,它是一个负载均衡客户端的抽象定义,下面我们就看看如何使用Spring Cloud提供的负载均衡器客户端接口来实现服务的消费. 引用之前的文章中构建的 ...

  7. 干货!一篇文章集合所有Linux基础命令,适合所有菜鸟学习和老手回顾!

    1 文件{ ls -rtl # 按时间倒叙列出所有目录和文件 ll -rt touch file # 创建空白文件 rm -rf 目录名 # 不提示删除非空目录(-r:递归删除 -f强制) dos2u ...

  8. 统计寄存器AX中1 的个数

    ;==================================== ; 统计寄存器AX中1 的个数 DATAS segment DATAS ends CODES segment START: ...

  9. webstorm报错Unescaped xml character解决方案1

    当idea认为你的书写格式不适合的时候便会报出Unescaped xml character提示,但不影响运行. 若想禁掉这个提示,可以尝试修改文档类型为:<!DOCTYPE html>

  10. html5中的progress兼容ie,制作进度条样式

    html5新增的progress标签用处很大,它可以制作进度条,不用像以前那样用css来制作进度条! 一.progress使用方法 progress标签很好使用,他有两个属性,value和max,va ...