当前位置:网站首页>LeetCode:1037. Effective boomerang - simple

LeetCode:1037. Effective boomerang - simple

2022-06-10 20:49:00 Kinght_ one hundred and twenty-three


subject

1037. Effective boomerang
Given an array points , among points[i] = [xi, yi] Express X-Y A point on the plane , If these points form a Boomerang Then return to true .

Boomerang Defined as a set of three points , These points Each are not identical 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

Tips :

  • points.length == 3
  • points[i].length == 2
  • 0 <= xi, yi <= 100

Their thinking

  • mathematics .
  • If three points are collinear , Then the cross product of any two vectors is equal to 0.

Code

class Solution:
    def isBoomerang(self, points: List[List[int]]) -> bool:
        
        v1 = (points[1][0] - points[0][0], points[1][1] - points[0][1])
        v2 = (points[2][0] - points[0][0], points[2][1] - points[0][1])
        return v1[0] * v2[1] - v1[1] * v2[0] != 0

Running results

原网站

版权声明
本文为[Kinght_ one hundred and twenty-three]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101943475029.html