最多有多少个点在一条直线上
来源:互联网 发布:淘宝平台争议处理规则 编辑:程序博客网 时间:2024/05/02 02:34
最多有多少个点在一条直线上
给出二维平面上的n个点,求最多有多少点在同一条直线上。
样例
给出4个点:(1, 2)
, (3, 6)
, (0, 0)
, (1, 3)
。
一条直线上的点最多有3个。
标签
/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */class Solution {public: /** * @param points an array of point * @return an integer */ int maxPoints(vector<Point>& points) { // Write your code here unordered_map<float, int> slopes;int maxp = 0, n = points.size();for (int i = 0; i < n; ++i) {slopes.clear();int duplicate = 1;for (int j = i + 1; j < n; ++j) {if (points[i].x == points[j].x && points[i].y == points[j].y) {++duplicate;continue;}float slope = (points[i].x == points[j].x) ? INT_MAX : (float)(points[i].y - points[j].y) / (points[i].x - points[j].x);++slopes[slope];}maxp = max(maxp, duplicate); //当全部点都为同一个点时,slopes这个map为空,因此需要这一句for (auto slope : slopes) {if (slope.second + duplicate > maxp)maxp = slope.second + duplicate;}}return maxp; }};
0 0
- 【LintCode】最多有多少个点在一条直线上
- LinkCode-最多有多少个点在一条直线上
- 最多有多少个点在一条直线上
- LintCode:最多有多少个点在一条直线上
- Lint_Code_最多有多少个点在一条直线上
- 最多有多少个点在一条直线上
- lintcode,最多有多少个点在一条直线上
- LintCode 最多有多少个点在一条直线上
- 最多有多少个点在一条直线上-LintCode
- lintcode最多有多少个点在一条直线上
- 解题报告:最多有多少个点在一条直线上
- lintcode(186)最多有多少个点在一条直线上
- [LintCode 186] 最多有多少个点在一条直线上(Python)
- 最多有多少个点在同一条直线上
- 186.Max Points on a Line-最多有多少个点在一条直线上(中等题)
- 网易互娱笔试题----最多有多少点在一条直线上
- 二维平面上最多有多少个点在同一条直线上
- [leetcode] Max Points on a Line 判断最多有多少个点在同一条直线上
- Linux 必备工具
- 用宏定义来完成位运算及复杂宏定义的解析方法
- Android API Guides---ListView
- SGU 140 Integer Sequences(扩展欧几里得)
- Java中的Stack栈和Heap堆的区别
- 最多有多少个点在一条直线上
- Class类中的一些方法
- Java中equals和==的区别
- 文章标题
- Linux内核分析——进程切换与系统一般执行过程
- 数字金额和汉字金额大写转换【前段+后台】
- python函数可以操纵外面的元素和列表吗?
- Java 加解密技术系列之 DH
- CSS3 选择器