【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 <= 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
【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 ...
随机推荐
- servlet学习之servletAPI编程常用的接口和类
ServletConfig接口: SevletConfig接口位于javax.servlet包中,它封装了servlet配置信息,在servlet初始化期间被传递.每一个Servlet都有且只有一个S ...
- Python3图片处理头像
一. 简介: Python3图片处理头像右上角类似QQ,微信右上角未读信息效果,其实没有实质作用,只是简单练习. 1. 环境: python3.5 random模块:生成随机数 PIL模块:图像处理模 ...
- 【转帖】超能课堂(186) CPU中的那些指令集都有什么用?
超能课堂(186)CPU中的那些指令集都有什么用? https://www.expreview.com/68615.html 不明觉厉 开始的地方 第一大类:基础运算类x86.x86-64及EM64T ...
- Coins —— POJ-1742
Time limit 3000 ms Memory limit 30000 kB Description People in Silverland use coins.They have coins ...
- Centos8 重启网卡方法
问题情况: 1.虚机centos8 修改为静态ip后,由于网卡网段变更,无法上网 2.最小化安装,没有ifconfig 3.firewalld,selinux关闭 4.ping 不通物理机 根本原因: ...
- php 如何生成path及其日常维护
php 如何生成path及其日常维护 path字段重要性不言而喻,在查询的时候,如果只用pid,查询效率会很低,增加path,查询效率大大提高,最起码不用递归查库了,重点是维护推荐关系的时候要维护pa ...
- 从入门到自闭之Python自定义模块
自定义模块 定义:一个文件就是一个模块(能被调用的文件,模块就是一个工具箱,工具就是函数) 作用: 将代码文家化管理,提高可读性,避免重复代码 拿来就用(避免重复造轮子),python中类库比较多,提 ...
- Codeforces 1203F2. Complete the Projects (hard version)
传送门 首先对于 $b>0$ 的工作显然有个贪心,把 $b>0$ 的按 $a$ 从小到大排序 把能做的都做了,然后得到一个最大等级 剩下就是考虑 $b<0$ 的工作了,看到数据显然可 ...
- oracle 使用问题
12541: 典型的listener.ora 文件内容: SID_LIST_LISTENER = (注册到监听器的service name所在区域) (SID_LIST = (SID_DE ...
- 判断页面是在移动端还是PC端打开的
$(function () { var curWwwPath = window.document.location.href; var pathName = window.document.locat ...