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: true
Example 2:

Input: [[1,1],[2,2],[3,3]]
Output: false
 

Note:

points.length == 3
points[i].length == 2
0 <= points[i][j] <= 100

这题,我直接用是否能组成三角形来解了。

class ValidBoomerang : public Solution {
  public:
    void Exec() {
        cout << "Case 1: "
             << isBoomerang(vector<vector<int>>{vector<int>{1, 10},
                                                vector<int>{20, 34},
                                                vector<int>{37, 92}})
             << std::endl;
    }
    bool isBoomerang(const vector<vector<int>> &points) {
        vector<int> dist;
        dist.push_back(
            (points[0][0] - points[1][0]) * (points[0][0] - points[1][0]) +
            (points[0][1] - points[1][1]) * (points[0][1] - points[1][1]));
        dist.push_back(
            (points[1][0] - points[2][0]) * (points[1][0] - points[2][0]) +
            (points[1][1] - points[2][1]) * (points[1][1] - points[2][1]));
        dist.push_back(
            (points[2][0] - points[0][0]) * (points[2][0] - points[0][0]) +
            (points[2][1] - points[0][1]) * (points[2][1] - points[0][1]));
        std::sort(dist.begin(), dist.end());

        return sqrt(double(dist[2])) < sqrt(double(dist[0])) + sqrt(double(dist[1]));
    }
};

共 0 条回复
暂时没有人回复哦,赶紧抢沙发
发表新回复

作者

sryan
today is a good day