leetcode 149. 直线上最多的点数 解题报告
给定一个二维平面,平面上有 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. 直线上最多的点数 解题报告的更多相关文章
- Java实现 LeetCode 149 直线上最多的点数
149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...
- Leetcode 149.直线上最多的点数
直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o ...
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
- [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. ...
- Max Points on a Line(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
随机推荐
- Java8函数之旅 (二) --Java8中的流
流与集合 众所周知,日常开发与操作中涉及到集合的操作相当频繁,而java中对于集合的操作又是相当麻烦.这里你可能就有疑问了,我感觉平常开发的时候操作集合时不麻烦呀?那下面我们从一个例子说起. 计 ...
- JavaScript中并非一切皆对象
对象是js中的基础以及核心,在js中有六种主要类型:string number boolean null undefined object 除了oject类型以为其他五种本身并非对象,null本身 ...
- BZOJ4008: [HNOI2015]亚瑟王(期望dp)
Time Limit: 20 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 1952 Solved: 1159[Submit][Status] ...
- Linux Centos 通过虚拟用户访问FTP的配置
Linux Centos 通过虚拟用户访问FTP的配置 实验需求: 让下面4个虚拟用户使用系统用户ftpvu的权限来连接到Linux FTP服务器,并确保都锁定在 自己的虚拟用户目录,不能切换到其他目 ...
- 交换机基础配置之三层交换机实现vlan间通信
我们以上面的拓扑图做实验,要求为pc1,pc2,pc3配置为vlan10,pc4,pc5,pc6配置为vlan20,pc7,pc8,pc9配置为vlan30 server0和server1配置为vla ...
- nginx 报错: nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
执行: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf https://www.cnblogs.com/codingcl ...
- 本地通过VMware Workstation创建虚拟机,配置网络环境
通过VMware Workstation创建虚拟机,系统安装完成后,需要配置相应网卡设置: 打开配置文件:vim /etc/sysconfig/network-scripts/ifcfg-ens33 ...
- 一次完整的http请求处理过程
一次完整的HTTP请求需要的7个步骤 HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接 在HTTP工作开始之前,Web浏览器首先 ...
- Mysql_Binary_Install_Scripts(采用二进制方式安装)
1.1 MYSQL实现代码 #!/bin/bash ######################################## #auth:wolf_dreams #time:2018-1 ...
- laravel查看执行的sql语句
方法一: 我们有时候想测试一段代码生产的 SQL 语句,比如: 我们想看 App\User::all(); 产生的 SQL 语句,我们简单在 routes.php 做个实验即可: //app/Http ...