【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 same straight line.
点和方向确定一条直线。
需要两重循环,第一重循环遍历起始点a,第二重循环遍历剩余点b。
a和b如果不重合,就可以确定一条直线。
对于每个点a,构建 斜率->点数 的map。
(1)b与a重合,以a起始的所有直线点数+1 (用dup统一相加)
(2)b与a不重合,a与b确定的直线点数+1
/**
* 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) {
if(points.empty())
return ;
else if(points.size() == )
return ; int ret = ;
for(int i = ; i < points.size(); i ++)
{//start point
int curmax = ; //points[i] itself
unordered_map<double, int> kcnt; // slope_k count
int vcnt = ; // vertical count
int dup = ; // duplicate added to curmax
for(int j = ; j < points.size(); j ++)
{
if(j != i)
{
double deltax = points[i].x - points[j].x;
double deltay = points[i].y - points[j].y;
if(deltax == && deltay == )
dup ++;
else if(deltax == )
{
if(vcnt == )
vcnt = ;
else
vcnt ++;
curmax = max(curmax, vcnt);
}
else
{
double k = deltay / deltax;
if(kcnt[k] == )
kcnt[k] = ;
else
kcnt[k] ++;
curmax = max(curmax, kcnt[k]);
}
}
}
ret = max(ret, curmax + dup);
}
return ret;
}
};

【LeetCode】149. Max Points on a Line的更多相关文章
- 【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- [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. ...
- [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. ...
- 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. ...
- 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. ...
- 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法/双指针 日期 题目地址:https://le ...
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...
随机推荐
- [转]Unicode和UTF-8的关系
Unicode和UTF-8的关系作者: 张军 原文地址: http://blog.renren.com/blog/284133452/485453790 今天中午,我突然想搞清楚Unicode和UTF ...
- 第一章 HttpClient的使用
1.http协议(这一块儿有时间的话会做记录) 2.常用的两种RPC方式 基于http协议:HttpClient和JDK自己的Http操作类 基于TCP或UDP协议:mina2和netty(这一部分以 ...
- go语言基础之切片和底层数组关系
1.切片和底层数组关系 示例: package main //必须有个main包 import "fmt" func main() { a := []int{0, 1, 2, 3, ...
- svn报错can only be performed on a version resource [at this time].
报错 can only be performed on a version resource [at this time]. 有的文件能提交,有的文件不能提交 猜想:是不是因为缓存问题方法:tea ...
- 只用CSS做到完全居中
我们都知道 margin:0 auto; 的样式能让元素水平居中,而 margin: auto; 却不能做到垂直居中……直到现在.但是,请注意!想让元素绝对居中,只需要声明元素高度,并且附加以下样式, ...
- Java之对象构造过程
先来运行一段代码 class A { public A() { init(); } public void init() { } public static void main(String[] ar ...
- C#.NET常见问题(FAQ)-在VS程序如何取消.vshost的进程
双击执行一个EXE程序,会有两个进程,程序关闭之后,貌似只能关闭你的程序,附加的vshost.exe仍然存在 在调试页面,改成release,同时取消最后一项启用承载进程 在生成页面,将高级选 ...
- ArcMap概化之消除真曲线
在地理国情项目中,异常折线检查结果中,有报错为:几何类型为esriGeometryCircularArc不合法,而属性表中几何类型(Shape)内容为 "面",这是为何? 作为GI ...
- C++生成十字绣图案(二) 面向对象
基本的十字绣线性生成中提供了判断下一步可以画的位置并且逐步生成的函数.以这些基本函数为基础,可以进行更多变化的图案设计. 为了方便的扩展,可以把线性生成写成一个类,以后的修改继承这个类. 头文件Bas ...
- 1z0-052 q209_6
6: You executed this command to create a temporary table: SQL> CREATE GLOBAL TEMPORARY TABLE repo ...