ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【leetcode】1232. Check If It Is a Straight Line

2019-10-21 15:02:14  阅读:466  来源: 互联网

标签:point Straight denominator numerator coordinates y1 Line x1 leetcode


题目如下:

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

 

标签:point,Straight,denominator,numerator,coordinates,y1,Line,x1,leetcode
来源: https://www.cnblogs.com/seyjs/p/11713607.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有