Max Points on a Line (Python)

【问题】

Given n points
on a 2D plane, find the maximum number of points that lie on the same straight line.

【思路】

对每一个点,分别计算这个点和其他所有点构成的斜率,具有相同斜率最多的点所构成的直线,就是具有最多点的直线。

【代码】

class Point:
	def __init__(self, a=0, b=0):
		self.x = a
		self.y = b

class Solution:
	# @param points, a list of Points
    # @return an integer
    def maxPoints(self, points):
    	length = len(points)
    	if length < 3:
    		return length
    	res = -1
    	for i in xrange(length):
    		slope = {'inf': 0}
    		samePointNum = 1
    		for j in xrange(length):
    			if i == j:
    				continue
    			elif points[i].x == points[j].x and points[i].y != points[j].y:
    				slope['inf'] +=1
    			elif points[i].x != points[j].x:
    				k = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x)
    				slope[k] = 1 if k not in slope.keys() else slope[k] + 1
    			else:
    				samePointNum += 1
    		res = max(res, max(slope.values()) + samePointNum)
    	return res
		

Max Points on a Line (Python)

时间: 2024-08-28 17:16:17

Max Points on a Line (Python)的相关文章

[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 the maximum number of points that lie on the same straight line. 解题思路:找到平面上在一条直线上最多的点.点在同一条直线上意味着这些点的斜率是一样的,那么可以考虑使用哈希表来解决,{斜率:[点1,点2]}这样的映射关系.这里有几个需要考虑

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma

【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. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

[LeetCode][JavaScript]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 same straight line. 求有多少点在一直线上. 粗暴地用二重循环遍历. 每一轮都构造一个哈希表,用来记录斜率,斜率k = (y1 - y2) / (x1 - x2). 注意到特殊情况: 1.两点重合,用countSamePoint记下重复的点,最后加到结果上. 2.两点与X轴平行,

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 same straight line. 这道题需要稍微转变一下思路,用斜率来实现,试想找在同一条直线上的点,怎么判断在一条直线上,唯一的方式也只有斜率可以完成,我开始没想到,后来看网友的思路才想到的,下面是简单的实现:其中有一点小技巧,利用map<double, int>来存储具有相同斜率

Max Points on a Line leetcode java

题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 这道题就是给你一个2D平面,然后给你的数据结构是由横纵坐标表示的点,然后看哪条直线上的点最多. (1)两点确定一条直线 (2)斜率相同的点落在一条直线上 (3)坐标相同的两个不同的点 算作2个点 利用HashMap,Key值存斜率,Value存此斜率下的点的个数.同时考虑特殊情况,如

[LeetCode OJ] 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.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&

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 same straight line. SOLUTION 1: 全部的点扫一次,然后计算每一个点与其它点之间的斜率. 创建一个MAP, KEY-VALUE是 斜率:线上的点的数目. 另外,注意重合的点,每次都要累加到每一条线上. 注意: 1. k = 0 + (double)(points[i].

LeetCode OJ - 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. 解题思路: 第一反应:枚举两个点组成的直线,然后看其他的点在不在这条直线上,在此过程中统计最大值.此思路的复杂度 O(n^3) 参考了网上的思路:枚举第一个点,用unordered_map来记录其余的点和这个点的斜率,若斜率相同则代表这些点在同一直线上.避免double问题,把斜率转化成化简