1037. Valid Boomerang 有效的回旋镖
作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/valid-boomerang/
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: trueExample 2:
Input: [[1,1],[2,2],[3,3]]Output: falseNote:
points.length == 3points[i].length == 20 <= points[i][j] <= 100
判断三个点是否不重叠,并且不在一个直线上。
不重叠很好说,三个点两两判断即可。
判断三个点是不是在一个直线上,那么三个点中任意两个点的连线之斜率是否相等(或者不存在)。用数学公式表示就是dx1 * dy2 == dx2 * dy1则在一条直线上。
C++代码如下:
class Solution {public: bool isBoomerang(vector<vector<int>>& points) { if (points[0][0] == points[1][0] && points[0][1] == points[1][1]) return false; if (points[0][0] == points[2][0] && points[0][1] == points[2][1]) return false; if (points[1][0] == points[2][0] && points[1][1] == points[2][1]) return false; int dx1 = points[1][0] - points[0][0]; int dy1 = points[1][1] - points[0][1]; int dx2 = points[2][0] - points[1][0]; int dy2 = points[2][1] - points[1][1]; return dx1 * dy2 != dx2 * dy1; }};2019 年 8 月 31 日 —— 赶在月底做个题

评论与交流