Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
判断给出的所有点是否共线,水题


 1 #1232
 2 #Runtime: 45 ms (Beats 45.11%)
 3 #Memory: 13.9 MB (Beats 29.35%)
 4 
 5 class Solution(object):
 6     def checkStraightLine(self, coordinates):
 7         """
 8         :type coordinates: List[List[int]]
 9         :rtype: bool
10         """
11         eps = 1e-6
12         for k in range(2, len(coordinates)):
13             if coordinates[0][0] == coordinates[1][0]:
14                 if coordinates[k][0] != coordinates[0][0]:
15                     return False
16             else:
17                 if abs(coordinates[0][0] - coordinates[k][0]) < eps or abs(1.0 * (coordinates[0][1] - coordinates[1][1]) / (coordinates[0][0] - coordinates[1][0]) - 1.0 * (coordinates[0][1] - coordinates[k][1]) / (coordinates[0][0] - coordinates[k][0])) > eps:
18                     return False
19         return True

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理