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 ...
随机推荐
- mongodb C++ Driver安装
前言 mongocxx官网地址 http://mongocxx.org/?jmp=docs 本文的安装版本是:mongocxx-r3.2.0.tar.gz . 参考文档安装过程http://mongo ...
- react-router 4.0版本使用笔记
react-router 4变化还是挺大的,看网上很多人遇到问题,都是基本用法的改变,所以这里记录一下. http://www.jianshu.com/p/d6727e8d81c4 1.react-r ...
- ES5拓展
一.JSON拓展 1.JSON.parse(str,fun):将JSON字符串转为js对象 两个参数:str表示要处理的字符串:fun处理函数,函数有两个参数,属性名.属性值 // 定义json字符串 ...
- hive 入门
hive-site.xml 配置 <configuration> <property> <name>javax.jdo.option.ConnectionURL&l ...
- 『Python基础-4』字符串
# 『Python基础-4』字符串 目录 1.什么是字符串 2.修改字符串 2.1 修改字符串大小 2.2 合并(拼接)字符串 2.3 使用乘号'*'来实现字符串的叠加效果. 2.4 在字符串中添加空 ...
- Golang通道的无阻塞读写的方法示例
无论是无缓冲通道,还是有缓冲通道,都存在阻塞的情况,但其实有些情况,我们并不想读数据或者写数据阻塞在那里,有1个唯一的解决办法,那就是使用select结构. 这篇文章会介绍,哪些情况会存在阻塞,以及如 ...
- HttpClient的Content-Type设置
HttpClient的Content-Type设置 最近在对接公司内容的一个云服务的时候,遇到一个问题,就是如果使用HttpClient如何设置post时候的Content-Type? public ...
- C#、C++、Java、Python 选择哪个好?
C#.C++.Java.Python 选择哪个好? 2019年03月06日 16:54:34 编程小火车 阅读数:214 首先排除Python,光动态语言一个理由,就已经万劫不复了.无论有多少所谓 ...
- struts2学习笔记四
一.contextMap中的数据操作 root根:List 元素1 元素2 元素3 元素4 元素5 contextMap:Map key value application Map key value ...
- 模仿淘宝首页写的高仿页面,脚本全用的原生JS,菜鸟一枚高手看了勿喷哈
自己仿照淘宝首页写的页面,仿真度自己感觉可以.JS脚本全是用原生JavaScript写得,没用框架.高手看了勿喷,请多多指正哈!先上网页截图看看效果,然后上源码: 上源码,先JavaScript : ...