题目:

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. 运行 命令框不记录打过的命令,重启后CMD里面是空的.上次打过的命令消失了.

    问题: 常要用到PING命令.在cmd中输入ping 202.103.44.150 /t (这是当地的电信DNS) 用这个查看网络是不是正常.正常情况下次点开始运行的时候,运行命令框中应该 会有上次打 ...

  2. 60. Permutation Sequence (String; Math)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. BroadcastReceiver接收电量变化的广播-------在代码中动态创建接受者

    本例为动态创建广播接收者即不是在AndroidManifest.xml文件中定义的广播接收着 代码: package com.qf.broadcastreceiver01; import androi ...

  4. 对于Android开发,啥是高级工程师?

    最近一直在思考自己的技术方向.新的技术永远都是层出不穷,kotlin,flutter,小程序,轻应用等等,但是作为一个老鸟,新的东西,永远都是学不完的,想在新的技术上迭代学习出一个新高度,而增加自己的 ...

  5. 解压查看二进制rpm包的方法

    详细参考: man cpio 具体方法: rpm2cpio qt5-qtbase-5.6.0-13.fc21.x86_64.rpm | cpio -dium 执行后可在当前目录查看 安装目录  etc ...

  6. extjs 学习一

    环境 : eclipse  ext  tomcat 6 将下载的extjs  解压后全部 导入到项目中 .使用时 <!-- 1.引入样式 2.引入库文件 ,底层驱动 3. ext-all--&g ...

  7. part1:8-远程登录Linux

    Linux远程登录 Linux系统中是通过ssh服务实现的远程登录功能.默认ssh服务开启了22端口,而且在安装完成系统时,这个服务已经安装,并且是开机启动的.所以不需要额外配置就能直接远程登录Lin ...

  8. Kendo UI中TreeView 放入tabstrip中,大数据量时超过边框的解决方案。

    参考http://www.kendoui.com/forums/ui/tabstrip/tabstip-with-treeview-treeview-breaking-out-of-tabstrip. ...

  9. 项目解析1、登录验证用户是否存在 储备知识 Python 之 decorator装饰器

    下面是我对 装饰器 这一小节的总结, 以及自己的理解. 注:[本文中的代码参考上述教程] 很多时候我会把Python的很多语法与C++相融合,在C++中,函数的名称即为函数的地址,我们可以通过定义成为 ...

  10. const变量指针赋值给非const类型的指针运行结果

    在c++可以定义一个const变量,然后把变量的值赋给一个非const指针,可以通过指针来改变const变量的值吗?下面的截图给出了答案