【Leetcode】1037. Valid Boomerang

题目地址:

https://leetcode.com/problems/valid-boomerang/

给定平面三个点的坐标,问其是否不在一条直线上。如果不在一条直线上则返回true。

两个向量 ( x 1 , y 1 ) , ( x 2 , y 2 ) (x_1,y_1),(x_2,y_2) (x1,y1),(x2,y2)共线等价于行列式 ∣ x 1 x 2 y 1 y 2 ∣ = 0 \left |\begin{array}{l} x_1 & x_2 \\ y_1 & y_2 \\ \end{array}\right|=0 x1y1x2y2=0代码如下:

public class Solution {
    
    
    public boolean isBoomerang(int[][] points) {
    
    
        int[] x = new int[2], y = new int[2];
        for (int i = 1; i < points.length; i++) {
    
    
            x[i - 1] = points[i][0] - points[0][0];
            y[i - 1] = points[i][1] - points[0][1];
        }
        
        return x[0] * y[1] != x[1] * y[0];
    }
}

时空复杂度 O ( 1 ) O(1) O(1)

猜你喜欢

转载自blog.csdn.net/qq_46105170/article/details/112798373