zoukankan      html  css  js  c++  java
  • 边工作边刷题:70天一遍leetcode: day 96-1

    Line Reflection

    要点:

    • 一个confusion:找到所有点的median为什么不行?重复的点的情况,另外unsorted找median没有max/min中点容易
    • 注意y轴对称要验证x
    • 公式:2*x-x0:x-x0就是对称边的delta,所以再加个x就是所求
    • 这题还要考虑y坐标相等:一个x可以有多个y点对应,所以要用adjList的结构

    错误点:

    • 取某一维向量应该用list comprehension,而不是用zip:zip只是对n个list,产生一个tuple的list
    • 因为中点不一定int,要用float来取mid point

    https://repl.it/CaKH/1

    from collections import defaultdict
    
    class Solution(object):
        def isReflected(self, points):
            """
            :type points: List[List[int]]
            :rtype: bool
            """
            if not points: return True
            xs = [p[0] for p in points]
            maxx, minx = max(xs), min(xs)
            x = (maxx + minx)/2.0 # error 1: float
            umap = defaultdict(list)
            for p in points:
                umap[p[0]].append(p[1])
    
            for p in points:
                if (2*x-p[0] not in umap) or (p[1] not in umap[2*x-p[0]]):
                    return False
    
            return True
            
    s = Solution()
    assert not s.isReflected([[1,1],[-1,-1]]), "False"
    assert s.isReflected([[0,0],[1,0]]), "True"
    
    
  • 相关阅读:
    2017.7.18 linux下ELK环境搭建
    2017.7.18 linux下用户、组和文件的操作
    action解耦方式
    action 耦合方式
    分页查询
    request,response,session
    Struts2
    hibernate中session,HQL,持久化,一对多,多对一
    hibernate 配置文件
    hibernate
  • 原文地址:https://www.cnblogs.com/absolute/p/5815918.html
Copyright © 2011-2022 走看看