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

2022-06-26 13:16:04 +08:00
 Zerek

提交记录

提交记录 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]]))

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

766 次点击
所在节点    全球工单系统
4 条回复
Moonkin
2022-06-26 21:56:39 +08:00
我也遇到过。。
Zerek
2022-06-26 22:57:40 +08:00
@Moonkin 那最后是怎么解决的呢。。
Moonkin
2022-07-13 10:59:42 +08:00
@Zerek 没解决。。一道题而已了,看自己代码和别人的也基本一样,就没管了。。
Zerek
2022-07-13 11:02:44 +08:00
@Moonkin 我解决了。。。他每次执行的时候虽然会__init__,但是并不会重新初始化整个 class
导致 class 内部字段还是有值的 手动初始化即可

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/862267

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX