Max Points on a Line
2015-08-28 20:44
281 查看
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Solution:
Solution:
/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ #define INF_MAX 0x3fffffff class Solution { public: int maxPoints(vector<Point>& points) { int len = points.size(); if(len <= 2) return len; unordered_map<double, int> um; int maxNum = 0; for(int i = 0; i < len; ++i) { um.clear(); int num = 0; for(int j = i + 1; j < len; ++j) { if(points[i].x == points[j].x && points[i].y == points[j].y) { num++; continue; } double slope = points[i].x == points[j].x ? INF_MAX : (points[j].y - points[i].y) * 1.0 / (points[j].x - points[i].x); if(!um.count(slope)) um[slope] = 2; else um[slope]++; } maxNum = max(maxNum, num + 1); for(auto iter = um.begin(); iter != um.end(); ++iter) if(maxNum < iter->second + num) maxNum = iter->second + num; } return maxNum; } };
相关文章推荐
- 事务
- nmake的调用方法
- mysqlsla快速入门
- 数据结构:数组的操作(C语言描述)
- php大力力 [034节] 今天做出系统后台页面的界面啦
- Android中你也许不知道的线性布局Layout_weight属性权重比例分配原理
- SHOW OPEN TABLES语法
- Hello World!
- c语言中头文件#include <stdio.h>和#include "stdio.h"有什么区别?
- comparator接口与Comparable接口的区别
- 模板方法模式
- 数据迁移经验总结——亿级别多表异构的数据迁移工作
- POJ 1125 裸最短路 Floyd
- Wait Condition 例子
- UI基础----常见控件的使用之Button
- linux下搭建java环境
- 大数据时代 统计学依然是数据分析灵魂
- Sahi 使用技巧3-快速编写Sahi脚本(1)
- Goods:注册页面保存User功能发送邮件以及激活功实现
- oracle 重启命令