zoukankan      html  css  js  c++  java
  • 【leetcode】1232. Check If It Is a Straight Line

    题目如下:

    You are given an array coordinatescoordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane. 

    Example 1:

    Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
    Output: true
    

    Example 2:

    Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
    Output: false

    Constraints:

    • 2 <= coordinates.length <= 1000
    • coordinates[i].length == 2
    • -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
    • coordinates contains no duplicate point.

    解题思路:初中几何知识,任意取两个点,解出方程y = k*x + b,然后判断其余点是否满足方程。

    代码如下:

    class Solution(object):
        def checkStraightLine(self, coordinates):
            """
            :type coordinates: List[List[int]]
            :rtype: bool
            """
            x1,y1 = coordinates[0]
            x2,y2 = coordinates[1]
            k_numerator = (y1-y2)
            k_denominator = (x1-x2)
            if k_denominator == 0:
                k_denominator = 1
            b = y1 - k_numerator/k_denominator*x1
    
            for i in range(2,len(coordinates)):
                x,y = coordinates[i]
                if y != k_numerator/k_denominator*x + b:
                    return False
            return True
  • 相关阅读:
    WCF三种通信方式
    Linux发布WebApi
    Supervisor Linux程序进程管理
    Centos安装Mongodb
    本地网址连不上远程mysql问题
    .Net之垃圾回收算法
    .Net之托管堆资源分配
    Centos7+ASP.Net Core 运行
    ASP .Net Core 使用 Dapper 轻型ORM框架
    转载 Jquery中AJAX参数详细介绍
  • 原文地址:https://www.cnblogs.com/seyjs/p/11713607.html
Copyright © 2011-2022 走看看