Powered By GitBook
1037.Valid Boomerang

1037.Valid Boomerang

难度:Easy
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
Example 1:
2
3
Input: [[1,1],[2,3],[3,2]]
4
Output: true
5
Example 2:
6
7
Input: [[1,1],[2,2],[3,3]]
8
Output: false
9
10
11
Note:
12
13
points.length == 3
14
points[i].length == 2
15
0 <= points[i][j] <= 100
Copied!
三点共线是斜率的比较,可以直接采用一次方程通用公式,也可以直接比较斜率,注意一下特殊情况即可。
1
class Solution {
2
public:
3
bool isBoomerang(vector<vector<int>>& points) {
4
int a=points[1][1]-points[0][1];
5
int b=points[1][0]-points[0][0];
6
int c=points[2][1]- points[0][1];
7
int d=points[2][0]-points[0][0];
8
// cout<<a<<" "<<b<<" "<<c<<" " << d<<endl;
9
if(b && d)
10
return a/float(b)!=c/float(d);
11
else if(b)
12
return c;
13
else if (d)
14
return a;
15
else
16
return false;
17
}
18
};
Copied!
执行用时 :4 ms, 在所有 C++ 提交中击败了92.14%的用户 内存消耗 :8.2 MB, 在所有 C++ 提交中击败了100.00%的用户
Last modified 2yr ago
Copy link