V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
JasonLaw
V2EX  ›  程序员

LeetCode 1743. Restore the Array From Adjacent Pairs 求助

  •  
  •   JasonLaw · 169 天前 · 335 次点击
    这是一个创建于 169 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我正在解决Restore the Array From Adjacent Pairs - LeetCode,我的 solution 并没有 pass 所有的 test case ,想问一下我的 solution 有什么问题,自己完全找不出来。🤐

    Thanks in advance.

    class Solution:
        def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
            num_to_neighbors = defaultdict(list)
            for u, v in adjacentPairs:
                num_to_neighbors[u].append(v)
                num_to_neighbors[v].append(u)
            
            head, next_num = next((num, neighbors[0]) for num, neighbors in num_to_neighbors.items() if len(neighbors) == 1)
            res = [head]
            while next_num:
                res.append(next_num)
                next_num = next((neighbor for neighbor in num_to_neighbors[next_num] if neighbor != res[-2]), None)
            return res
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2830 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:10 · PVG 23:10 · LAX 08:10 · JFK 11:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.