最多有多少个点在一条直线上
来源:互联网 发布:python远程监控服务器 编辑:程序博客网 时间:2024/05/02 02:53
题目:给出二维平面上的n个点,求最多有多少点在同一条直线上。
例子:给出4个点:(1, 2), (3, 6), (0, 0), (1, 3)。一条直线上的点最多有3个。
方法:取定一个点points[i], 遍历其他所有节点, 然后统计斜率相同的点数(用map(float, int) 记录斜率及其对应点数,取map中点数最多的斜率),并求取最大值即可。
class Solution {public: int maxPoints(vector<Point> &points) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. unordered_map<float,int> mp; int maxNum = 0; for(int i = 0; i < points.size(); i++) { mp.clear(); mp[INT_MIN] = 0; int duplicate = 1; for(int j = 0; j < points.size(); j++) { if(j == i) continue; if(points[i].x == points[j].x && points[i].y == points[j].y) { duplicate++; continue; } float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x); mp[k]++; } unordered_map<float, int>::iterator it = mp.begin(); for(; it != mp.end(); it++) if(it->second + duplicate > maxNum) maxNum = it->second + duplicate; } return maxNum; }};
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 判断最多有多少个点在同一条直线上
- android之Handler用法
- java解析xml的几种方式 Dom4j
- 关于hadoop与jstl冲突问题java.lang.AbstractMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/e
- 使用缓冲区复制文本文件及readLine()原理
- iOS 计算年龄
- 最多有多少个点在一条直线上
- Install Htop (Linux Process Monitoring) for RHEL, CentOS & Fedora
- 关于项目在新导入时所有有httpservlet的地方均报错的解决办法
- cvte面试
- HDU 5437 Alisha’s Party (2015年长春赛区网络赛A题)
- 顺序表的基本运算
- 【第3周 项目1 - 顺序表的基本运算】
- IOS网络状态检查
- 面向对象(继承02)-第19天