【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- ...
随机推荐
- 不明白的sizeof(enum)数据结构存储问题
不明白的sizeof(enum)数据结构存储问题 typedef struct weekday_st { enum week {sun=123456789,mon,tue,wed,thu,fri,sa ...
- Rabbit MQ UI 重置 用户名 密码
新增admin用户 rabbitmqctl add_user newadmin s0m3p4ssw0rd rabbitmqctl set_user_tags newadmin administrato ...
- 第二章 企业项目开发--maven父子模块
2.1.maven父子模块 在实际开发中,我们基本都会用maven父子分模块的方式进行项目的开发. 2.2.实际操作 2.2.1.手工建立一个ssmm0的文件夹,并在该文件夹中加入一个pom.xml文 ...
- Python源码学习七 .py文件的解释
Python源码太复杂了... 今天看了下对.py文件的parse, 云里雾里的 py文件是最简单的, 在python的交互式窗口 import这个模块 a = 10 print(a) 开始分析,堆栈 ...
- Android 时间日期Widget 开发详解
桌面Widget其实就是一个显示一些信息的工具(现在也有人开发了一些有实际操作功能的widget.例如相机widget,可以直接桌面拍照).不过总的来说,widget主要功能就是显示一些信息.我们今天 ...
- 浅析Linux线程调度
在Linux中,线程是由进程来实现,线程就是轻量级进程( lightweight process ),因此在Linux中,线程的调度是按照进程的调度方式来进行调度的,也就是说线程是调度单元.Linux ...
- 剑指offer-序列化二叉树
请实现两个函数,分别用来序列化和反序列化二叉树 以前提交的内存超出了,可能现在要用非递归实现了 #include<iostream> #include<math.h> #inc ...
- Gson 使用总结 高级用法
Gson基本用法 参考:http://www.jianshu.com/p/e740196225a4 Gson提供了fromJson() 和toJson() 两个直接用于解析和生成的方法,前者实现反序列 ...
- 同时启动多个Tomcat服务器
以下步骤能够同时启动两个tomcat:1.特别要注意:不要设置CATALINA_HOME 2.分别修改安装目录下的conf子目录中的server.xml文件: a.修改http访问端口(默认为8080 ...
- [置顶] 利用Global.asax的Application_BeginRequest 实现url 重写 无后缀
利用Global.asax的Application_BeginRequest 实现url 重写 无后缀 <%@ Application Language="C#" %> ...