forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_points_on_a_line.py
More file actions
30 lines (28 loc) · 1.06 KB
/
max_points_on_a_line.py
File metadata and controls
30 lines (28 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
class Solution:
# @param {int[]} points an array of point
# @return {int} an integer
def maxPoints(self, points):
# Write your code here
ret = 0
if points:
for i in xrange(len(points)):
slopes = {}
for j in xrange(len(points)):
if i != j:
slope = self.calcSlope(points[i], points[j])
if slope not in slopes:
slopes[slope] = 2 # 第一次要记录两个点
else:
slopes[slope] += 1
for slope, count in slopes.items():
if count > ret:
ret = count
if ret == 0:
ret = 1
return ret
def calcSlope(self, point_x, point_y):
if point_x.x == point_y.x:
return 2147483647
else: # 如果浮点数不能解决问题就用最大公约数!
return float(point_x.y - point_y.y) / float(point_x.x - point_y.x)