Max Points on a Line
来源:互联网 发布:js获取radio是否选中 编辑:程序博客网 时间:2024/06/07 16:04
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
思路:通过求最大公约数来求直线的斜率,并且记录下来,斜率相同即在同一直线上。
class Solution {public:int maxPoints(vector<Point>& points) { int res = 0; for (int i = 0; i < points.size(); ++i) { map<pair<int, int>, int> m; int duplicate = 1; for (int j = i + 1; j < points.size(); ++j) { if (points[i].x == points[j].x && points[i].y == points[j].y) { ++duplicate; continue; } int dx = points[j].x - points[i].x; int dy = points[j].y - points[i].y; int d = gcd(dx, dy); ++m[{dx / d, dy / d}]; } res = max(res, duplicate); for (auto it = m.begin(); it != m.end(); ++it) { res = max(res, it->second + duplicate); } } return res; } int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } };
阅读全文
0 0
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points On a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- max points on a line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- H265(HEVC) nal 单元头介绍及rtp发送中的fu分组发送详解
- J2EE系列之Spring4学习笔记(三)--IOC详解(依赖注入)
- 迭代器模式【Iterator Pattern】
- Android中的数据存储(三):SQLite
- Kotlin基础教程-代码编写约定
- Max Points on a Line
- Mesos+Zookeeper+Marathon的Docker管理平台部署记录(1)
- C/C++头文件一览
- 人工智能-基于规则的专家系统(基础知识)
- windows自定义SetTimer
- 算法--腾讯面试:单词游戏,谁会赢?
- 29.输入一棵二叉树,判断该二叉树是否是平衡二叉树。
- Hero In Maze
- 国产程序员容易发音错误的计算机相关英文单词