码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-面试题16.03交点

时间:2020-04-13 10:34:06      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:def   mic   intersect   试题   elf   面试题   leetcode   star   面试   

题目描述:

技术图片

 

 方法:

class Solution:
    def intersection(self, start1, end1, start2, end2):
        x1, y1, x2, y2, x3, y3, x4, y4 = *start1, *end1, *start2, *end2
        det = lambda a, b, c, d: a * d - b * c
        d = det(x1 - x2, x4 - x3, y1 - y2, y4 - y3)
        p = det(x4 - x2, x4 - x3, y4 - y2, y4 - y3)
        q = det(x1 - x2, x4 - x2, y1 - y2, y4 - y2)
        if d != 0:
            lam, eta = p / d, q / d
            if not (0 <= lam <= 1 and 0 <= eta <= 1): return []
            return [lam * x1 + (1 - lam) * x2, lam * y1 + (1 - lam) * y2]
        if p != 0 or q != 0: return []
        t1, t2 = sorted([start1, end1]), sorted([start2, end2])
        if t1[1] < t2[0] or t2[1] < t1[0]: return []
        return max(t1[0], t2[0])

 

leetcode-面试题16.03交点

标签:def   mic   intersect   试题   elf   面试题   leetcode   star   面试   

原文地址:https://www.cnblogs.com/oldby/p/12689654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!