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

LeetCode 1743. Restore the Array From Adjacent Pairs 求助

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

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

    我正在解决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   ·   我们的愿景   ·   实用小工具   ·   1056 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com