leetcode 1232. Check If It Is a Straight Line



Easy

159 场周赛第一题

题意

You are given an array coordinates, coordinates[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.

思路

这道题很简单,就是看所有的点之间的斜率是否相等就行。结果一开始没想到这个,走了很多误区,结果半小时才AC,真是菜!?

python
class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        k = []
        for i in range(1,len(coordinates)):
            count=1
            dy= (coordinates[i][1]-coordinates[i-1][1])
            dx=(coordinates[i][0]-coordinates[i-1][0])
            if dx==0:
                count=0
            else:count=dy/dx
            k.append(count)
        k=set(k)
        if len(k)==1:
            return True
        else:return False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值