V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
工单节点使用指南
• 请用平和的语言准确描述你所遇到的问题
• 厂商的技术支持和你一样也是有喜怒哀乐的普通人类,尊重是相互的
• 如果是关于 V2EX 本身的问题反馈,请使用 反馈 节点
Zerek
V2EX  ›  全球工单系统

昨天在 leetcode 刷题的时候遇到的问题,本地测试&WEB 上手动填写用例答案正确,提交答案时同样的用例答案显示错误.

  •  
  •   Zerek · 2022-06-26 13:16:04 +08:00 · 751 次点击
    这是一个创建于 667 天前的主题,其中的信息可能已经有所发展或是发生改变。

    提交记录

    提交记录 11 / 64 个通过测试用例 状态:解答错误 最后执行的输入:6 [[0,1],[2,3],[4,5]]

    代码(Python3)

    from typing import List
    
    
    class Solution:
        lines = []
    
        class PointLine:
    
            def __init__(self, p1, p2):
                self.point1 = p1
                self.point2 = p2
                pass
    
        def countPairs(self, n: int, edges: List[List[int]]) -> int:
            count = 0
            already = set()
            for x in edges:
                self.lines.append(self.PointLine(x[0], x[1]))
            for x in range(n):
                for y in range(n):
                    if x == y or already >= {x.__str__() + "|" + y.__str__(), }:
                        continue
                    already.add(x.__str__() + "|" + y.__str__())
                    already.add(y.__str__() + "|" + x.__str__())
                    if not self.find_way(x, y):
                        count += 1
    
            return count
    
        def find_way(self, p1, p2) -> bool:
            for n in self.lines:
                if ((n.point1 == p1 and n.point2 == p2)
                        or
                        (n.point1 == p2 and n.point2 == p1)):
                    return True
            return False
    
    
    print(Solution().countPairs(6, [[0, 1], [2, 3], [4, 5]]))
    

    知道写的不是很优雅,这两天才学的 可是这是啥情况...

    4 条回复    2022-07-13 11:02:44 +08:00
    Moonkin
        1
    Moonkin  
       2022-06-26 21:56:39 +08:00
    我也遇到过。。
    Zerek
        2
    Zerek  
    OP
       2022-06-26 22:57:40 +08:00
    @Moonkin 那最后是怎么解决的呢。。
    Moonkin
        3
    Moonkin  
       2022-07-13 10:59:42 +08:00
    @Zerek 没解决。。一道题而已了,看自己代码和别人的也基本一样,就没管了。。
    Zerek
        4
    Zerek  
    OP
       2022-07-13 11:02:44 +08:00
    @Moonkin 我解决了。。。他每次执行的时候虽然会__init__,但是并不会重新初始化整个 class
    导致 class 内部字段还是有值的 手动初始化即可
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1492 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 17:12 · PVG 01:12 · LAX 10:12 · JFK 13:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.