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

思路:对于某一点来说,在经过该点的直线中选取节点数量最多的直线;对于全局来说,必定是某个局部点满足条件的直线之一=>局部最优解也是全局最优解=>贪心法。

/**
* Definition for a point.
* struct 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) {
int ret = ;
int subMax = ;
double slope;
int sameCounter = ;
int zeroCounter = ;
map<double,int> count;
for(int i = ; i < points.size(); i++){
for(int j = i+; j < points.size(); j++){
if(points[j].x==points[i].x && points[j].y==points[i].y) sameCounter++;
else if(points[j].x-points[i].x == ){
zeroCounter++;
if(zeroCounter>subMax) subMax = zeroCounter;
}
else{
slope = (double) (points[j].y-points[i].y)/(points[j].x-points[i].x);
count[slope]++;
if(count[slope]>subMax) subMax=count[slope];
}
}
count.clear();
zeroCounter = ;
if(subMax+sameCounter > ret) ret = subMax+sameCounter;
subMax = ;
sameCounter = ;
}
return ret;
}
};

149. Max Points on a Line (Array; Greedy)的更多相关文章

  1. 【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 ...

  2. [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. ...

  3. Java for 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. ...

  4. 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数

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

  5. leetcode 149. Max Points on a Line --------- java

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

  6. 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 li ...

  7. 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 ...

  8. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  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 line. ...

随机推荐

  1. shutil模块---文件,文件夹复制、删除、压缩等处理

    shutil模块:高级的文件,文件夹,压缩包处理 拷贝内容 # shutil.copyfileobj(open('example.ini','r'),open('example.new','w')) ...

  2. 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析 Week6 20165233

    Exp4 恶意代码分析 实验内容 一.基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控. 使用wi ...

  3. C++官方文档-静态成员

    #include <iostream> using namespace std; class Dummy { public: static int n; int x; Dummy() : ...

  4. c++官方文档-指针

    #include<stdio.h> #include<iostream> #include<queue> #include<map> #include& ...

  5. 练手nginx反向代理和负载均衡apache实战

    先说下原理性的 什么是反向代理 用户访问域名  域名的指向到nginx  nginx把请求转发到apache  apache处理后 返回给用户 整套的逻辑 对于用户来说  就是访问域名 然后返回  没 ...

  6. JS获取最终样式

    在使用jqery时,操作什么都很方便,比如获取CSS样式,直接.css加样式名就可以获取你要的,但是JS,就麻烦点,因为有兼容问题,要做兼容,而jqery都是做好了的, 下面就是使用JS获取CSS样式 ...

  7. windows自带杀毒防火墙

    windows自带杀毒防火墙 Windows Defender FireWall

  8. starling 第一天

    flashplayer_27_sa_debug: https://files.cnblogs.com/files/dt1991/flashplayer_27_sa_debug.rar flashpla ...

  9. ABAP-关于 LUW

    转载:https://www.cnblogs.com/liaojunbo/archive/2011/07/12/2103554.html 假设MAIN PROGRAM(调用程序)为MAIN,其所在的为 ...

  10. 从底层谈WebGIS 原理设计与实现(二):探究本质,WebGIS前端地图显示之地图比例尺换算原理

    从底层谈WebGIS 原理设计与实现(二):探究本质,WebGIS前端地图显示之地图比例尺换算原理 作者:naaoveGI…    文章来源:http://www.cnblogs.com/naaove ...