lintcode-186-最多有多少个点在一条直线上
186-最多有多少个点在一条直线上
给出二维平面上的n个点,求最多有多少点在同一条直线上。
样例
给出4个点:(1, 2), (3, 6), (0, 0), (1, 3)。
一条直线上的点最多有3个。标签
哈希表 领英 数学
思路
从第一个开始,求出此点与其它点的斜率(注意斜率会可能会不存在),斜率相同的点在同一直线上,相同的点(重复出现的点)与任何点均在同一直线上
为了遍历方便,使用 map 保存斜率与点数的映射关系
code
/**
* 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:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
// Write your code here
int size = points.size();
if (size <= 0) {
return 0;
}
map<double, int> map;
int maxPoints = 0;
for (int i = 0; i < size; i++) {
map.clear();
int samek = 0, maxk = 0;
for (int j = 0; j < size; j++) {
// 同一点
if (points[i].x == points[j].x && points[i].y == points[j].y) {
samek++;
}
// 斜率不存在
else if (points[i].x == points[j].x && points[i].y != points[j].y) {
maxk++;
}
// 斜率存在
else {
double k = double(points[i].y - points[j].y) / double(points[i].x - points[j].x);
map[k]++;
}
}
std::map<double, int>::iterator it;
for (it = map.begin(); it != map.end(); it++) {
maxPoints = maxPoints > it->second + samek ? maxPoints : it->second + samek;
}
maxPoints = maxPoints > maxk + samek ? maxPoints : maxk + samek;
}
return maxPoints;
}
};
lintcode-186-最多有多少个点在一条直线上的更多相关文章
- lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- [LintCode] 最多有多少个点在一条直线上
/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(i ...
- 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. ...
- [Swift]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. ...
- leetcode 149. 直线上最多的点数 解题报告
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o +------- ...
- Leetcode 149.直线上最多的点数
直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o ...
- Max Points on a Line(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- Java实现 LeetCode 149 直线上最多的点数
149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
随机推荐
- 第一篇:百问网ubuntu安装注意事项和部分配置
目录 一.开启虚拟化技术 二.ubuntu部分设置 一.开启虚拟化技术 64位机,需要使用cpu-z.SecurAble软件来检查:CPU是否支持VT虚拟化技术 cpu-z使用(软件) 第一步:以 ...
- 007---TCP VS UDP
- .net core 中后台获取前台 数据(post)的方法
[HttpPost] public async Task<JsonResult> EditPoint() { Stream reqStream = Request.Body; string ...
- 机器学习实战:KNN代码报错“AttributeError: 'dict' object has no attribute 'iteritems'”
报错代码: sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) 解决 ...
- VINS(五)非线性优化与在线标定调整
首先根据最大后验估计(Maximum a posteriori estimation,MAP)构建非线性优化的目标函数. 初始化过程通过线性求解直接会给出一个状态的初值,而非线性优化的过程关键在于求解 ...
- JavaSE打开windows文件
第一个参数表示用什么程序打开,第二个参数表示文件的路径 例一: //用记事本打开d:/test.txt文件 Process p = java.lang.Runtime.getRuntime().exe ...
- macOS 10.13 High Sierra PHP开发环境配置
命令:sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems/MySQLCOM ...
- MongoDB 安装 增删改查
MongoDB 一 介绍 1.高性能的数据存储解决方案是大多数大型Web应用程序和服务的核心.后端数据库负责存储一切东西,从用户账户的信息到购物车中的商品,以及博客和评论数据等.好的Web应用需要 ...
- connect by 语句
create table tb_menu( id number(10) not null, --主键id titlevarchar2(50), --标题 parent number(10) --par ...
- HIS系统两种收费模式比较:前计费和后计费
一.药品 a.前计费:审核(临时医嘱)或者分解(长期医嘱)计费 退费处理方式,1)如果是还未发药,则护士站直接退费;2)如果药房已经发药,则护士站发出退费申请,由护士拿着药品去药房退药退费. b.后计 ...