题目描述
中文
回旋镖定义为一组三个点,这些点各不相同且不在一条直线上。
给出平面上三个点组成的列表,判断这些点是否可以构成回旋镖。
英文
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.
示例1
输入:[[1,1],[2,3],[3,2]]
输出:true
示例2
输入:[[1,1],[2,2],[3,3]]
输出:false
提示
points.length == 3
points[i].length == 2
0 <= points[i][j] <= 100
解题思路
- 首先判重。
if((points[0][0] == points[1][0] && points[0][1] == points[1][1])
|| (points[0][0] == points[2][0] && points[0][1] == points[2][1])
|| (points[1][0] == points[2][0] && points[1][1] == points[2][1])){
return false;
}
- 然后判断是否在一条直线
else {
int x = points[2][0],y = points[2][1];
int x1 = points[0][0],y1 = points[0][1];
int x2 = points[1][0], y2 = points[1][1];
if((y-y1)*(x2 - x1) == (y2 - y1)*(x - x1))
return false;
}
- 然后就没了
完整代码
class Solution {
public boolean isBoomerang(int[][] points) {
if((points[0][0] == points[1][0] && points[0][1] == points[1][1])
|| (points[0][0] == points[2][0] && points[0][1] == points[2][1])
|| (points[1][0] == points[2][0] && points[1][1] == points[2][1])){
return false;
}
else {
int x = points[2][0],y = points[2][1];
int x1 = points[0][0],y1 = points[0][1];
int x2 = points[1][0], y2 = points[1][1];
if((y-y1)*(x2 - x1) == (y2 - y1)*(x - x1))
return false;
}
return true;
}
}