[LeetCode] 1037. Valid Boomerang

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

Constraints:

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

有效的回旋镖。

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

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/valid-boomerang
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道初中数学题,判断三个点是否在同一条直线上。判断三个点是否在同一条直线上其实就是判断每两个点形成的直线的斜率是否相同。根据斜率公式我们知道 斜率 = (x2 - x1) / (y2 - y1)。我们可以根据这个公式求两点之间的斜率,但是为了防止分母为0的情况,我们可以通过交叉相乘的结果是否相等来判断斜率是否相同。

时间O(1)

空间O(1)

Java实现

1 class Solution {
2     public boolean isBoomerang(int[][] points) {
3         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]);
4     }
5 }

 

LeetCode 题目总结

posted @ 2021-09-10 23:28  CNoodle  阅读(33)  评论(0编辑  收藏  举报