【leetcode】1232. Check If It Is a Straight Line
题目如下:
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: trueExample 2:
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: falseConstraints:
2 <= coordinates.length <= 1000coordinates[i].length == 2-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4coordinatescontains 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
【leetcode】1232. Check If It Is a Straight Line的更多相关文章
- 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...
- 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 日期 题目地址:https://leetcod ...
- 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【leetcode】1250. Check If It Is a Good Array
题目如下: Given an array nums of positive integers. Your task is to select some subset of nums, multiply ...
- 【leetcode】1003. Check If Word Is Valid After Substitutions
题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...
- 【leetcode】958. Check Completeness of a Binary Tree
题目如下: Given a binary tree, determine if it is a complete binary tree. Definition of a complete binar ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
随机推荐
- webpack打包vue项目之后怎么启动&注意事项
参考路径:https://blog.csdn.net/cn_yaojin/article/details/80164477 参考路径:https://www.imooc.com/article/323 ...
- PowerShell学习记录
一.简介——连接 Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境.你可以把它看成是命令行提示符cmd.exe的扩充,不对,应当是颠覆. powershe ...
- Java学习-4
面向对象的三大特征之一:继承性 主要解决问题:共性抽取 继承关系当中的特点:子类可以拥有父类的“内容”,子类还可以拥有自己专属的内容 定义一个父类的格式(就是定义一个普通类的格式): Public c ...
- Linux 磁盘卷扩容
首先识别磁盘,成功之后会显示在/dev下 [root@oracle01 ~]# fdisk /dev/sda ## /dev/sda为通过fdisk -l 查看到的物理磁盘(第一行) Welcome ...
- 针对yarn的8088端口攻击
参考: https://www.wangbokun.com/%E8%BF%90%E7%BB%B4/2019/09/02/%E6%8C%96%E7%9F%BF%E7%97%85%E6%AF%92.htm ...
- js获取url上的指定参数
function getAllUrlParams(url) { // get query string from url (optional) or window var queryString = ...
- deepin 15.10.1 GTX1060 NVIDIA 驱动安装,双屏显示问题记录
有一段时间没有用Linux了.由于买了个4k的戴尔显示屏,在deepin系统上无法用,从昨晚到现在,总于解决了我的问题! 问题1:无法直接在深度的显卡驱动管理器哪里直接切换,网上看到很多人都有这个问题 ...
- Fluent API
fluent api用于onmodelcreating里,可以实现比attribute更强更灵活的配置 public partial class StoreDBContext : DbContext ...
- RSA 加密长度计算公式
The length of data that can be encrypted using RSA is determined primarily by the size of the key yo ...
- 04 Websocket和Websocketed
一.web socket事件和方法 有了HTTP协议为什么还需要Websocket这种协议呢?因为HTTP协议发起的通信只能通过客户端发起,然后服务端才可以将消息回应到客户端.因此HTTP协议做不到服 ...

