leetcode-1037-easy

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

思路一:用斜率比较

    public static 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;
        }

        double x1 = points[1][0] - points[0][0];
        double x2 = points[2][0] - points[0][0];
        double y1 = points[1][1] - points[0][1];
        double y2 = points[2][1] - points[0][1];

        if (y1 == 0 && y2 == 0) {
            return false;
        } else if (y1 == 0 || y2 == 0) {
            return true;
        }

        double slopA = x1 / y1;
        double slopB = x2 / y2;

        return slopA != slopB;
    }
posted @ 2023-02-27 21:22  iyiluo  阅读(12)  评论(0)    收藏  举报