首页 > 其他分享 >leetcode-1037-easy

leetcode-1037-easy

时间:2023-02-27 21:33:31浏览次数:35  
标签:return && double 1037 points easy y1 y2 leetcode

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

标签:return,&&,double,1037,points,easy,y1,y2,leetcode
From: https://www.cnblogs.com/iyiluo/p/17161988.html

相关文章