LeetCode题解(1037):判断三个点是否为直线(Python)
发布日期:2021-06-29 19:55:16
浏览次数:4
分类:技术文章
本文共 794 字,大约阅读时间需要 2 分钟。
题目:(简单)
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | – | – | 44ms (59.85%) |
Ans 2 (Python) | – | – | 48ms (37.45%) |
Ans 3 (Python) |
LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。
解法一(距离法):
def isBoomerang(self, points: List[List[int]]) -> bool: a = pow((points[0][0] - points[1][0]) ** 2 + (points[0][1] - points[1][1]) ** 2, 0.5) b = pow((points[0][0] - points[2][0]) ** 2 + (points[0][1] - points[2][1]) ** 2, 0.5) c = pow((points[1][0] - points[2][0]) ** 2 + (points[1][1] - points[2][1]) ** 2, 0.5) return not max([a, b, c]) * 2 == sum([a, b, c])
解法二(斜率法):
def isBoomerang(self, points: List[List[int]]) -> bool: x1, y1 = points[0][0], points[0][1] x2, y2 = points[1][0], points[1][1] x3, y3 = points[2][0], points[2][1] return not (x3 - x1) * (y2 - y1) == (x2 - x1) * (y3 - y1)
转载地址:https://dataartist.blog.csdn.net/article/details/107102182 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月21日 01时05分22秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
java开发操作系统面试题
2019-04-30
java异常知识点总结
2019-04-30
java反射怎么使用
2019-04-30
java设计模式
2019-04-30
java集合详细讲解
2019-04-30
C++ 函数名重载、 标准库、条件编译
2019-04-30
C++ 枚举类型、C++ 数组类型、C++ 结构类型、C++ 联合类型、C++ 指针类型
2019-04-30
Python使用的脚本
2019-04-30
解决windows电脑蓝屏的方法
2019-04-30
Python3学习之路~异常处理
2019-04-30
(纯)虚函数和多态
2019-04-30
0、NULL和nullptr
2019-04-30
allocator
2019-04-30
const、底层和顶层
2019-04-30
explicit限制隐式转换
2019-04-30
C++ 类和对象
2019-04-30
数组、new、malloc的内存分配情况
2019-04-30
C++ 构造函数和析构函数
2019-04-30