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 共线点个数的更多相关文章

  1. [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. ...

  2. [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. ...

  3. [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. ...

  4. 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. ...

  5. 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. ...

  6. 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. ...

  7. 【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 ...

  8. 【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 ...

  9. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

随机推荐

  1. 【Miscalculation UVALive - 6833 】【模拟】

    题目分析 题目讲的是给你一个串,里面是加法.乘法混合运算(个人赛中误看成是加减乘除混合运算),有两种算法,一种是乘法优先运算,另一种是依次从左向右运算(不管它是否乘在前还是加在前). 个人赛中试着模拟 ...

  2. ACAG 0x02-4 费解的开关

    ACAG 0x02-4 费解的开关 对于这道题,我们不难发现如下性质: 每个位置之多被点击一次: 点击的先后顺序不影响结果: 若确定了第$1$行,则接下来可能的点击方案就只有$1$种.具体原因是:当第 ...

  3. python的tkinter,能画什么图?

    今天从下午忙到现在,睡觉. 这个能绘点图的. import json import tkinter as tk from tkinter import filedialog from tkinter ...

  4. Docker 部署 vue 项目

    Docker 部署 vue 项目 Docker 作为轻量级虚拟化技术,拥有持续集成.版本控制.可移植性.隔离性和安全性等优势.本文使用Docker来部署一个vue的前端应用,并尽可能详尽的介绍了实现思 ...

  5. Dubbo源码分析:Serialization

    背景 顺序化逻缉处理! 类图 获取Serialization对象时序图 序列化

  6. janusgraph-创建索引出现GraphIndexStatusReport[success=false, indexName='mixedvlabel', targetStatus=[REGISTERED], notConverged={vlabel=INSTALLED}, converged={}, elapsed=PT1M0.07S]

    参考网址: https://www.cnblogs.com/Uglthinx/p/9630779.html 原因:我的是事务没有完全关闭 解决办法: 创建一个混合索引: // 在graph中有事务执行 ...

  7. 安装cuda及之后更新环境变量的方法

    安装参考:https://www.cnblogs.com/fanfzj/p/8521728.html 更新环境变量: 将 CUDA.CUPTI 和 cuDNN 安装目录添加到 %PATH% 环境变量中 ...

  8. Build Post Office II

    Description Given a 2D grid, each cell is either a wall 2, an house 1 or empty 0 (the number zero, o ...

  9. CSP2019 J组 游记

    结果 分数出来了.100+100+10+35=245. 一等线230,擦着边进一等. (点击图片放大) 期待明年s组的表现. 第一轮 不就是初赛吗?擦边轻松水过去! 第二轮 Day -14 停两周晚自 ...

  10. 异常过滤器的好坏(CLR)

    为什么有些语言支持它们而另一些不支持呢?把它们加到我的新语言里是个好主意吗?我应该什么时候使用过滤器和catch/rethrow?就像很多事情一样,异常过滤器有好的一面也有坏的一面… 什么是异常过滤器 ...