LeetCode题解(0356):直线镜像(Python)
发布日期:2021-06-29 20:09:19 浏览次数:3 分类:技术文章

本文共 985 字,大约阅读时间需要 3 分钟。

题目:(中等)

标签:几何、数学、哈希表

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N l o g N ) O(NlogN) O(NlogN) O ( N ) O(N) O(N) 48ms (98.11%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:    def isReflected(self, points: List[List[int]]) -> bool:        count = collections.defaultdict(set)  # 以点(x,y)的y为key,x为值        for x, y in points:            count[y].add(x)        val = None        for x, lst in count.items():            lst = list(sorted(lst))            if len(lst) % 2 == 1:                mid = lst[len(lst) // 2]            else:                mid = (lst[len(lst) // 2 - 1] + lst[len(lst) // 2]) / 2            # 检查自身是否相对中线对称            left, right = 0, len(lst) - 1            while left < right:                if lst[right] - mid != mid - lst[left]:                    return False                left += 1                right -= 1            # 检查中线是否一致            if val is None:                val = mid            elif val != mid:                return False        return True

转载地址:https://dataartist.blog.csdn.net/article/details/109816508 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(0359):日志速率限制器(Python)
下一篇:LeetCode题解(0340):至多包含K个不同字符的最长子串(Python)

发表评论

最新留言

不错!
[***.144.177.141]2024年04月17日 11时41分25秒