题目:

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

解题思路:

1,在所有点中选定一个点作为中心点,然后再求剩下的点到该中心点的斜率,如果斜率相同的点表示在同一直线上

2,如果剩下点中有与中心点相同的点,则记下相同点的个数,然后直接跳过,继续下一个点到中心点斜率的求解

3,为了防止重复计算,当以节点i作为中心节点时,剩余的点表示为数组中i点后面的点

实现代码:

#include <iostream>
#include <vector>
#include <map>
#include <limits>
#include <unordered_map>
using namespace std; /* */
struct Point {
int x;
int y;
Point() : x(), y() {}
Point(int a, int b) : x(a), y(b) {}
}; class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.size() == )
return ;
int max = ;
map<double, int> umap;
for(int i = ; i < points.size(); i++)
{
int tmp_max = ;//当已第i个点位中心时,同一直线上点数最大值
umap.clear();
int repeat = ;//与i点相同点的个数
for(int j = i+; j < points.size(); j++)
{
double slope = numeric_limits<double>::infinity();
if(points[j].x != points[i].x)
slope = double(points[j].y - points[i].y) / (points[j].x - points[i].x);
else if(points[j].y == points[i].y)//与中心点相同的点
{
repeat++;
continue;
}
umap[slope]++;//到中心点斜率相同的点数++,这里umap中存在该斜率,则直接将该斜率对应的值++,否则先添加,再++
if(umap[slope] > tmp_max)
tmp_max = umap[slope];
}
tmp_max += repeat;//以i为中心点出发的每一条直线上的点数都应该加上repeat,因为与i点相同的点在所有从i出发的直线上
if(tmp_max > max)
max = tmp_max;//更新全局最大值 }
return max + ; //之前所求的每一条直线上的点数都没有加上该直线的中心点,所以这里要加上1
}
}; int main(void)
{
Point ps[] = {{,},{,},{,}};
int len = sizeof(ps) / sizeof(Point);
vector<Point> points(ps, ps+len);
Solution solution;
int ret = solution.maxPoints(points);
cout<<ret<<endl;
return ;
}

LeetCode149:Max Points on a Line的更多相关文章

  1. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  2. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  3. [LintCode] Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  4. 【LeetCode】149. Max Points on a Line

    Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...

  5. LeetCode: Max Points on a Line 解题报告

    Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...

  6. [leetcode]149. Max Points on a Line多点共线

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  7. Max Points on a Line leetcode java

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  8. 【Max Points on a Line 】cpp

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  9. LeetCode(149) Max Points on a Line

    题目 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...

随机推荐

  1. haproxy 参数说明

    说明: 1.haproxy的配置段有"global","defaults","listen","frontend"和&q ...

  2. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  3. Linux 下 FastDFS v5.08 分布式文件系统的安装

    一.系统安装目录 源代码包目录 /data/wwwroot libevent安装目录 /usr/local/libevent FastDFS安装目录 /data/fastdfs nginx安装目录 / ...

  4. sublime text3 配置

    1.在sublime text官网下载安装文件并安装 http://www.sublimetext.com/ 2.安装sublime的package control 参考网址:https://pack ...

  5. Java8 改进的匿名内部类:

    1.匿名内部类适合创建那种只需要一次使用的类 2.匿名内部类定义格式: new 实现接口() | 父类构造器(实参列表){ //匿名内部类类体部分 } 3.从上面定义格式可以看出,匿名内部类必须实现一 ...

  6. PAT 1003 我要通过!(20)(代码+思路)

    1003 我要通过!(20)(20 分)提问 "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下 ...

  7. Android窗口背景的优化

    视图有背景,每个窗口也是有背景的.每一Activity是一个窗口,每一个Activity都有不同得背景.界面的绘画顺序如下:窗口——跟视图 ——子视图.当我们的跟视图已经覆盖了整个窗口的时候 ,程序还 ...

  8. Spring Boot 集成 Mybatis(druid 数据库连接池 以及 分页配置)

    MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射,目前很大一部分互联网.软件公司都在使用这套框架 关于Mybatis-Generator的下载可以到这个地址:http ...

  9. 2018.10.20 NOIP模拟 面包(数学期望)

    传送门 把方差的式子拆开. 方差=平方的期望-期望的平方. 显然只用维护点对的个数和总方案数就行了. 利用分步的思想来统计. 要统计覆盖一个矩形(x1,y1,x2,y2)(x1,y1,x2,y2)(x ...

  10. 2018.07.06 POJ1556 The Doors(最短路)

    The Doors Time Limit: 1000MS Memory Limit: 10000K Description You are to find the length of the shor ...