[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.
给一个由n个点组成的2D平面,找出最多的同在一条直线上的点的个数。
共线点的条件是斜率一样,corn case:点相同;x坐标相同。
Java:
public class Solution {
public int maxPoints(Point[] points) {
int res = 0;
for (int i = 0; i < points.length; ++i) {
Map<Map<Integer, Integer>, Integer> m = new HashMap<>();
int duplicate = 1;
for (int j = i + 1; j < points.length; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
++duplicate; continue;
}
int dx = points[j].x - points[i].x;
int dy = points[j].y - points[i].y;
int d = gcd(dx, dy);
Map<Integer, Integer> t = new HashMap<>();
t.put(dx / d, dy / d);
m.put(t, m.getOrDefault(t, 0) + 1);
}
res = Math.max(res, duplicate);
for (Map.Entry<Map<Integer, Integer>, Integer> e : m.entrySet()) {
res = Math.max(res, e.getValue() + duplicate);
}
}
return res;
}
public int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
}
Python:
class Point:
def __init__(self, a=0, b=0):
self.x = a
self.y = b class Solution(object):
def maxPoints(self, points):
"""
:type points: List[Point]
:rtype: int
"""
max_points = 0
for i, start in enumerate(points):
slope_count, same = collections.defaultdict(int), 1
for j in xrange(i + 1, len(points)):
end = points[j]
if start.x == end.x and start.y == end.y:
same += 1
else:
slope = float("inf")
if start.x - end.x != 0:
slope = (start.y - end.y) * 1.0 / (start.x - end.x)
slope_count[slope] += 1 current_max = same
for slope in slope_count:
current_max = max(current_max, slope_count[slope] + same) max_points = max(max_points, current_max) return max_points
C++:
class Solution {
public:
int maxPoints(vector<Point>& points) {
int res = 0;
for (int i = 0; i < points.size(); ++i) {
map<pair<int, int>, int> m;
int duplicate = 1;
for (int j = i + 1; j < points.size(); ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
++duplicate; continue;
}
int dx = points[j].x - points[i].x;
int dy = points[j].y - points[i].y;
int d = gcd(dx, dy);
++m[{dx / d, dy / d}];
}
res = max(res, duplicate);
for (auto it = m.begin(); it != m.end(); ++it) {
res = max(res, it->second + duplicate);
}
}
return res;
}
int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 149. Max Points on a Line 共线点个数的更多相关文章
- [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] 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. ...
- 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. ...
- 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
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 ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
随机推荐
- selenium获取元素信息方法(转载)
1.获取当前页面的Url函数 方法:current_url 实例: driver.current_url 2.获取元素坐标 方法:location 解释:首先查找到你要获取元素的,然后调用locati ...
- 鸡尾酒排序Cocktail Sort (双向冒泡排序)
鸡尾酒排序 鸡尾酒排序思路,先从左边开始进行冒泡排序,第一趟冒泡排序完,最大值在的数组的最右端,然后进行第二趟排序,第二趟排序从右边开始排序,第二趟结束时,最小值在数组最左端,以此类推,每一趟排序完都 ...
- C#锐利体验2读书笔记
匿名方法,迭代,匿名方法允许我们以一种“内联”的方法来编写方法代码;匿名方法是直接与委托实例化相关联的,使委托实例化更加直观方便.匿名方法的几个相关问题--参数列表,--返回值,--外部变量. add ...
- C++中的类所占内存空间总结(转)
类所占内存的大小是由成员变量(静态变量除外)决定的,成员函数(这是笼统的说,后面会细说)是不计算在内的. 摘抄部分: 成员函数还是以一般的函数一样的存在.a.fun()是通过fun(a.this)来调 ...
- 使用GCOV进行代码覆盖率统计
GCOV是随GCC一起发布的用于代码覆盖率统计的工具,一般配合其图形化工具LCOV一起使用. 一.安装 GCOV不需要单独安装,LCOV下载后执行sudo make install即可完成安装. 二. ...
- LeetCode 446. Arithmetic Slices II - Subsequence
原题链接在这里:https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 题目: A sequence of numbers is ...
- Xamarin移动开发之路
Xamarin入门 1.Xamarin开发及学习资源 2.Xamarin安装及调试 Xamarin.Forms 1.入门 [快速入门] 2.XAML 3.应用程序基础知识 [辅助功能]eg:大类型.高 ...
- BZOJ1034 ZJOJ2008 泡泡堂BNB
BZOJ1034 ZJOJ2008 泡泡堂BNB Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表 队由n名选手组成,比赛 ...
- kafka 创建消费者报错
kafka-console-consumer.sh --zookeeper master:2181,slave1:2181,slave2:2181 --topic test --from-beginn ...
- Noip2019暑期训练2
题目名称 骑士遍历 和谐俱乐部 农场派对 对称二叉树 存盘文件名 knight Beautiful party tree 输入文件名 knight.in Beautiful.in party.in ...