Max Points on a Line
来源:互联网 发布:伪装苹果在线软件 编辑:程序博客网 时间:2024/06/03 21:20
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
#define INFINITE_SLOP999999.99struct Point { int x; int y; Point() : x(0), y(0) {} Point(int a, int b) : x(a), y(b) {} };class Solution {public: int maxPoints(vector<Point> &points) {if (points.empty())return 0;int size = points.size();int maxPoints = 1;for(int i = 0; i < size ; i++){map <float, int> slopes;int numSamePoints = 0;for(int j = i ; j < size ; j++){if (points[i].x == points[j].x && points[i].y == points[j].y){numSamePoints++;continue;}float slope = (points[i].x == points[j].x ? INFINITE_SLOP : (float)(points[i].y - points[j].y) / (float)(points[i].x - points[j].x));if (slopes.find(slope) == slopes.end())slopes.insert(pair<float, int> (slope, 1));elseslopes[slope]++;}for(map<float, int>::iterator it = slopes.begin();it != slopes.end(); ++it)maxPoints = it->second + numSamePoints > maxPoints ? it->second + numSamePoints: maxPoints;maxPoints = numSamePoints > maxPoints ? numSamePoints : maxPoints;}return maxPoints;}};
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
- 第八章 8.2.1节练习
- CentOS 安装PHP5.5
- 每日linux命令学习之ps
- 05.抽象工厂模式【ABSTRACT FACTORY PATTERN】
- [JavaScript]-----String类型
- Max Points on a Line
- 杭电1022 Train Problem I(栈)
- android 项目地址
- Python图表软件包ChartDirector的安装和使用
- 15个nosql数据库
- hdu 1496 Equations(数学:暴力|| HASH)
- 这就是生活--1
- iOS UIButtom控件的使用方法
- HDU1042 N! (大精度)