Accepted Code in Python


  • 0
    M
    class Solution:
    # @param points, a list of Points
    # @return an integer
    def gcd(self, a, b):
    	if a < b:
    		a, b = b, a
    	while b != 0:
    		temp = a % b
    		a = b
    		b = temp
    	return a
    def slope(self, l):
    	if l[0]**2 + l[1]**2 == 0:
    		return '0 0'
    	else:
    		g = self.gcd(l[0], l[1])
    		r = [l[0]/g, l[1]/g]
    		if r[0]<0:
    			r = [-i for i in r]
    		return ' '.join([str(r[0]), str(r[1])])
    def maxPoints(self, points):
    	if len(points) <= 2:
    		return len(points)
    	m = 0
    	while len(points) >= 2:
    		center = points.pop()
    		slope = [self.slope([p.x - center.x, p.y - center.y]) for p in points]
    		times = {}
    		ztimes = 0
    		for s in slope:
    			if s == '0 0':
    				ztimes += 1
    			elif s in times:
    				times[s] += 1
    			else:
    				times[s] = 1
    		if times:
    			n = ztimes + max(times.values()) + 1
    		else:
    			n = ztimes + 1
    		if m < n:
    			m = n
    	return m

  • 0
    S

    Take this as reference.


  • 0
    M

    I copied this code ,and submitted the code.But I got Runtime Error.

    Sorry , it's my fault. The code is Accepted


  • 0
    S

    It is Accepted. Pasting this python code needs to fix its indentation.


  • 0
    M

    Thank you . I find this problem , too . And I want to edit my comment.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.