当前位置:网站首页>Force deduction solution summary 1037- effective boomerang

Force deduction solution summary 1037- effective boomerang

2022-06-12 02:12:00 Lost summer

Directory links :

Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog

GitHub Synchronous question brushing items :

https://github.com/September26/java-algorithms

Original link : Power button


describe :

Given an array  points , among  points[i] = [xi, yi]  Express X-Y A point on the plane , If these points form a   Boomerang   Then return to  true .

Boomerang   Defined as a set of three points , These points   Each are not identical   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
 

Tips :

points.length == 3
points[i].length == 2
0 <= xi, yi <= 100

source : Power button (LeetCode)
link :https://leetcode.cn/problems/valid-boomerang
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

Their thinking :

*  Their thinking :
*  First, compare the three points to see if they overlap .
*  Then judge whether all three points are X Same axial direction .
*  Finally, the three points are subtracted by two ,Y Axis difference divided by X Axis difference , See if it's the same .

Code :

    public boolean isBoomerang(int[][] points) {
        int[] point0 = points[0];
        int[] point1 = points[1];
        int[] point2 = points[2];
        if (point0[0] == point1[0] && point0[1] == point1[1]) {
            return false;
        }
        if (point0[0] == point2[0] && point0[1] == point2[1]) {
            return false;
        }
        if (point1[0] == point2[0] && point1[1] == point2[1]) {
            return false;
        }
        if (point0[0] == point1[0] || point0[0] == point2[0]) {
            return point0[0] != point1[0] || point0[0] != point2[0];
        }
        return (double)(point0[1] - point1[1]) / (double)(point0[0] - point1[0]) != (double)(point0[1] - point2[1]) / (double)(point0[0] - point2[0]);
    }

原网站

版权声明
本文为[Lost summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120202541850.html