1. 程式人生 > >【leetcode】1037. Valid Boomerang

【leetcode】1037. Valid Boomerang

obj pan etc type bsp NPU list 是否 leetcode

題目如下:

A boomerang is a set of 3 points that are all distinct and not in a straight line.

Given a list of three points in the plane, return whether these points are a boomerang.

Example 1:

Input: [[1,1],[2,3],[3,2]]
Output: true

Example 2:

Input: [[1,1],[2,2],[3,3]]
Output: false

Note:

  1. points.length == 3
  2. points[i].length == 2
  3. 0 <= points[i][j] <= 100

解題思路:本題就是判斷三點是否在一條直線上,判斷的方法也很簡單,任意從這三個點中選取兩組點對,如果這兩組的點對的斜率相同,則在同一直線上。

代碼如下:

class Solution(object):
    def isBoomerang(self, points):
        """
        :type points: List[List[int]]
        :rtype: bool
        
""" return (points[0][1]-points[1][1]) * (points[1][0]-points[2][0]) != (points[0][0]-points[1][0]) * (points[1][1]-points[2][1])

【leetcode】1037. Valid Boomerang