当前位置:网站首页>LeetCode:1037. 有效的回旋镖————简单

LeetCode:1037. 有效的回旋镖————简单

2022-06-10 19:43:00 Kinght_123


题目

1037. 有效的回旋镖
给定一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点,如果这些点构成一个 回旋镖 则返回 true 。

回旋镖 定义为一组三个点,这些点 各不相同 且 不在一条直线上 。

示例 1:
输入:points = [[1,1],[2,3],[3,2]]
输出:true

示例 2:
输入:points = [[1,1],[2,2],[3,3]]
输出:false

提示:

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

解题思路

  • 数学。
  • 如果三点共线,那么任意两个向量叉乘等于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

运行结果

原网站

版权声明
本文为[Kinght_123]所创,转载请带上原文链接,感谢
https://tim1304662247.blog.csdn.net/article/details/125178315