LeetCode149: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,在所有点中选定一个点作为中心点,然后再求剩下的点到该中心点的斜率,如果斜率相同的点表示在同一直线上
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的更多相关文章
- 【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 ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- [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. ...
- 【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 ...
- 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 ...
- [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. ...
- 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 ...
- 【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 ...
- 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 ...
随机推荐
- NBU 还原LINUX ORACLE数据库(CRM)
[root@oraclea ~]# su - oracle [oracle@oraclea ~]$ bplist -C oracle-db1 -s // -e // -k oracle_oracle- ...
- oracle杀掉执行的死循环存储过程
select * from v$db_object_cache where locks > 0 and pins > 0 and type='PROCEDURE'; select b.si ...
- hibernate经常报的几个不起眼的错误, 都是因为不细心或者手滑了输入有误造成了
最近会经常用到hibernate了, 也经常报错, 看着屏幕上一根大红条是非常不爽的, 这几天集合了一下经常报的错误, 整合一下放到这里, 以后再出现这个错误直接去解决就好了 1, org.hiber ...
- python的协程和异步io【select|poll|epoll】
协程又叫做微线程,协程是一种用户态的轻量级的线程,操作系统根本就不知道协程的存在,完全由用户来控制,协程拥有自己的的寄存器的上下文和栈,协程调度切换时,将寄存器上下文和栈保存到其他地方,在切换回来后, ...
- ios 工具大全,最全框架
https://www.jianshu.com/p/e280f3348156 [链接]文件分享 - 网盘分享https://share.weiyun.com/5A1aura
- 关于block元素和inline元素
呃...这个会不会太基础了.最近在复习,所以基础知识也不能够忽略. 根据HTML 4.01 规范,其描述如下(http://www.w3.org/TR/html401/struct/global.ht ...
- vmware14中安装centos7并使用docker发布spring-boot项目
1.vmare中centos7安装(同一路由器无线网络下) 1.1选择桥接模式 1.2修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens33(这里不一 ...
- ecplise导入工程出现乱码的解决方案
eclipse之所以会出现乱码问题是因为eclipse编辑器选择的编码规则是可变的.一般默认都是UTF-8或者GBK,当从外部导入的一个工程时,如果该工程的编码方式与eclipse中设置的编码方式不同 ...
- 学美工、平面设计、UI设计,哪个有前途?
首先,在分析学美工.平面设计.UI设计,哪一个更有前途的时候,先要对三类设计有一个大概的了解. 比如“学美工.平面设计.UI设计是什么”等知识,才能更好地去进行选择. 01 阐述美工.平面设计.UI设 ...
- hook进程
https://www.cnblogs.com/Leo_wl/p/3311279.html https://blog.csdn.net/u013761036/article/details/65465 ...