描述
Given an array points where points[i] = [xi, yi] represents a point on the X-Y plane, return true if these points are a boomerang.
A boomerang is a set of three points that are all distinct and not in a straight line.
Example 1:
Input: points = [[1,1],[2,3],[3,2]]
Output: true
Example 2:
Input: points = [[1,1],[2,2],[3,3]]
Output: false
Note:
points.length == 3
points[i].length == 2
0 <= xi, yi <= 100
解析
根据题意判断这三个点能否组成一个三角形,用海伦公式就可以解决,不懂的百度,这里我 Runtime Error 七八次,主要就是对于 float 类型的数的比较和计算上频繁出错,还有就是网上的各种解答也都过时,因为题目中边界条件发生了变化,都是坑啊。
解答
class Solution(object):
def isBoomerang(self, points):
"""
:type points: List[List[int]]
:rtype: bool
"""
x1,y1 = points[0][0],points[0][1]
x2,y2 = points[1][0],points[1][1]
x3,y3 = points[2][0],points[2][1]
a = math.sqrt((x2-x1)**2 + (y2-y1)**2)
b = math.sqrt((x3-x1)**2 + (y3-y1)**2)
c = math.sqrt((x3-x2)**2 + (y3-y2)**2)
p = (a+b+c)/2
S = math.sqrt(p*abs(p-a)*abs(p-b)*abs(p-c))
if S<0.0001 or S==0:
return False
return True
运行结果
Runtime: 16 ms, faster than 88.76% of Python online submissions for Valid Boomerang.
Memory Usage: 13.5 MB, less than 7.87% of Python online submissions for Valid Boomerang.
原题链接:leetcode.com/problems/va…
您的支持是我最大的动力