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. 神奇的null和undefined

    在JavaScript中,有两个特殊的类型存在,它们都只有一个值,分别null和undefined,之所以将它们放在一块,是因为在使用方面它们有很多相似之处. 相同点 在JavaScript中,nul ...

  2. [转]LAMP(Linux-Apache-MySQL-PHP)网站架构

    本文转自 http://www.williamlong.info/archives/1908.html LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框 ...

  3. TOM带你玩充电 篇三:15款5号电池横评及选购建议——南孚金霸王小米宜家耐时品胜一个都逃不了

    双鹿电池的几个版本 理论上来说性价比:绿骑士>金骑士>黑骑士>蓝骑士 绿骑士和金骑士都很不错.哪个便宜买哪个. 小米性价比虽然最高,但是超市买不到. 蓝骑士是普通碳性电池,黑骑士是高 ...

  4. PHP-Socket服务端客户端发送接收通信实例详解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://fighter.blog.51cto.com/1318618/1533957 So ...

  5. 微信公众平台测试帐号的注册与使用(自己的服务器<---->微信后台<---->测式公众号)

    打开注册的网址:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 用手机微信扫描网页左边的二维码,然后在手机上确认即可: 至此 ...

  6. ExtJS模版技术

    学习ExtJS一段时间以后,大家基本都会对于一些显示数据的组件不太符合需求,可能自己需要的组件在ExtJS里面不存在,这是大家基本就会使用Html属性,直接使用Html进行绘制页面数据展现. 但是,使 ...

  7. png,jpg,gif这些图片格式解释一下,分别什么时候用,webp呢

    gif图形交换格式,索引颜色格式,颜色少的情况下,产生的文件极小,支持背景透明,动画,图形渐进,无损压缩(适合线条,图标等),缺点只有256种颜色 jpg支持上百万种颜色,有损压缩,压缩比可达180: ...

  8. Java内存原型分析:基本知识

    转载: Java内存原型分析:基本知识 java虚拟机内存原型 寄存器:我们在程序中无法控制 栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中 堆:存放用new产生的数据 静 ...

  9. python入门-用户输入

    1 input()函数来实现用户输入,程序在等待输入的时候会终止,获取用户的输入后继续 message = input("tell me something,and I will repre ...

  10. 0_Simple__MultiGPU

    使用多台 GPU 进行计算 ▶ 源代码.使用不同的流来控制不同 GPU 上的运算任务. #include <stdio.h> #include <timer.h> #inclu ...