LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
题目: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.
这道题需要稍微转变一下思路,用斜率来实现,试想找在同一条直线上的点,怎么判断在一条直线上,唯一的方式也只有斜率可以完成,我开始没想到,后来看网友的思路才想到的,下面是简单的实现:
其中有一点小技巧,利用map<double, int>来存储具有相同斜率值的的点的数量,简洁高效。
/*
Definition for a point
*/
// struct Point {
// int x;
// int y;
// Point():x(0),y(0) {}
// Point (int a, int b):x(0), y(0) {}
// }; int maxPoints(vector<Point> &points) {
if (points.empty())
return ;
if (points.size() <= )
return points.size(); int numPoints = points.size();
map<double, int> pmap; //存储斜率-点数对应值
int numMaxPoints = ; for (int i = ; i != numPoints - ; ++i) {
int numSamePoints = , numVerPoints = ; //针对每个点分别做处理 pmap.clear(); for (int j = i + ; j != numPoints; ++j) {
if(points[i].x != points[j].x) {
double slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x);
if (pmap.find(slope) != pmap.end())
++ pmap[slope]; //具有相同斜率值的点数累加
else
pmap[slope] = ;
}
else if (points[i].y == points[j].y)
numSamePoints ++; //重合的点
else
numVerPoints ++; //垂直的点 }
map<double, int>::iterator it = pmap.begin();
for (; it != pmap.end(); ++ it) {
if (it->second > numVerPoints)
numVerPoints = it->second;
}
if (numVerPoints + numSamePoints > numMaxPoints)
numMaxPoints = numVerPoints + numSamePoints;
}
return numMaxPoints + ;
}
LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard的更多相关文章
- [leetcode]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...
- LeetCode:Max Points on a Line
题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...
- [LeetCode] 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 ...
- lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- [LeetCode] 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: 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 ...
- 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. ...
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- [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. ...
随机推荐
- 数据导入Excel时,出现ole error 800AC472这个错误,怎么解决。
我也出现过这个问题 在生成报表的时候不要动EXCEL中的任何单元格 让它完成保存就可以了 或者是把office 2003 删除下载一个office 2000就可以解决 据说是版本兼容的问题 不是高手 ...
- Spark升级--在CDH-5.15.1中添加spark2
一.环境准备 jdk-1.8+scala-2.11.X+python-2.7 二.创建目录 mkdir -p /opt/cloudera/csd 修改权限 chown cloudera-scm:clo ...
- IP路由配置之---------debugging调试
实验设备:华三设备N台加一个PC 步骤一,打开屏幕输出开关,开启控制台对系统信息的监视功能 <H3C>terminal debugging #<H3C>terminal mon ...
- Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.2/gradle-3
参考 https://blog.csdn.net/verkery6/article/details/80797705
- AngularJS学习笔记(二)
一.AngularJS Select(选择框) 1.使用 ng-options 创建选择框 <div ng-app="myApp" ng-controller="m ...
- WIN10在loadrunner安装或path4插件安装时遇到的管理员阻止程序运行---解决方案(可用)
方法1:关闭用户账户控制1.“win+x”进入控制面板,选择安全性与维护,在左侧更改windows smartscreen筛选器设置,选择"不执行任何操作",单击确定即可.2.“w ...
- pythone函数基础(8)内置函数学习
内置函数学习# sorted# map# filter# max# sum# round# chr# ord# dir# bool# eval# exec# zipimport mathres = m ...
- 48-Python 安装pyautogui失败解决办法
转载自:https://www.cnblogs.com/SH170706/p/9809830.html Python 安装pyautogui 在Python中使用PyAutoGui模拟键盘和鼠标操作 ...
- Entity Framework Core: A second operation started on this context before a previous operation completed
我这边报错是因为函数声明的是async void 而实现中有多个task任务,导致的线程不安全
- 阮一峰大神的快排?刚才还在纠结sort()的我!真是个小傻瓜
看到这个标题之后 我毫不犹豫的点进去了 趁现在不忙我赶紧把代码写到了我的小本本上好好研究研究 (写的就不放进来了 有点丑) 研究了下 第一反应 明明能用sort()解决的 为什么非要写这么一大串 但 ...