zoukankan      html  css  js  c++  java
  • [LeetCode] 1037. 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

    有效的回旋镖。

    回旋镖定义为一组三个点,这些点各不相同且不在一条直线上。

    给出平面上三个点组成的列表,判断这些点是否可以构成回旋镖。

    示例 1:

    输入:[[1,1],[2,3],[3,2]]
    输出:true
    示例 2:

    输入:[[1,1],[2,2],[3,3]]
    输出:false

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/valid-boomerang
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道初中数学题,判断三个点是否在同一条直线上。判断三个点是否在同一条直线上其实就是判断每两个点形成的直线的斜率是否相同。根据斜率公式我们知道 斜率 = (x2 - x1) / (y2 - y1)。我们可以根据这个公式求两点之间的斜率,但是为了防止分母为0的情况,我们可以通过交叉相乘的结果是否相等来判断斜率是否相同。

    时间O(1)

    空间O(1)

    Java实现

    1 class Solution {
    2     public boolean isBoomerang(int[][] points) {
    3         return (points[0][0] - points[1][0]) * (points[0][1] - points[2][1]) != (points[0][0] - points[2][0]) * (points[0][1] - points[1][1]);
    4     }
    5 }

    LeetCode 题目总结

  • 相关阅读:
    C++静态库与动态库(转)
    Tornado异步
    Yacc与Lex
    云数据库
    linux如何查看端口被谁占用
    Innodb Double Write
    MySQL GTIDs(global transaction identifiers)
    Java并发编程:线程池的使用
    Oracle 建立索引及SQL优化
    解决redhat linux下IP地址可以ping通,域名无法ping通问题
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15253418.html
Copyright © 2011-2022 走看看