題目

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

題目大意

迴旋鏢定義為一組三個點,這些點各不相同且不在一條直線上。給出平面上三個點組成的列表,判斷這些點是否可以構成迴旋鏢。

解題思路

  • 判斷給出的 3 組點能否滿足迴旋鏢。
  • 簡單題。判斷 3 個點組成的 2 條直線的斜率是否相等。由於斜率的計算是除法,還可能遇到分母為 0 的情況,那麼可以轉換成乘法,交叉相乘再判斷是否相等,就可以省去判斷分母為 0 的情況了,代碼也簡潔成一行了。

參考代碼

package leetcode

func isBoomerang(points [][]int) bool {
	return (points[0][0]-points[1][0])*(points[0][1]-points[2][1]) != (points[0][0]-points[2][0])*(points[0][1]-points[1][1])
}