IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 解题 | 从相邻元素对还原数组 -> 正文阅读

[数据结构与算法]解题 | 从相邻元素对还原数组

题目

存在一个由 n 个不同元素组成的整数数组 nums ,但你已经记不清具体内容。好在你还记得 nums 中的每一对相邻元素。

给你一个二维整数数组 adjacentPairs ,大小为 n - 1 ,其中每个 adjacentPairs[i] = [ui, vi] 表示元素 ui 和 vi 在 nums 中相邻。

题目数据保证所有由元素 nums[i] 和 nums[i+1] 组成的相邻元素对都存在于 adjacentPairs 中,存在形式可能是 [nums[i], nums[i+1]] ,也可能是 [nums[i+1], nums[i]] 。这些相邻元素对可以按任意顺序出现。

返回原始数组nums 。如果存在多种解答,返回其中任意一个即可。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs

在这里插入图片描述
在这里插入图片描述

结果

语言:Python3
执行用时:408 ms
内存消耗:68.8 MB

代码

参考官方的两个提示写了两个版本,第一次尝试用递归的思想,提交后显示Timeout,所以考虑避免递归,采用索引存储的方式来减少耗时。

class Solution:

    def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
        n = len(adjacentPairs) + 1
        res = []

        # Find the first element of nums - it will only appear once in adjacentPairs.
        temp1 = {}  # Count the number of occurrences.
        temp2 = {}  # Save the index(es) of each element in adjacentPairs.
        for i in range(n-1):
            a, b = adjacentPairs[i]
            temp1[a] = temp1.get(a, 0) + 1
            if temp1[a] > 1: 
                temp2[a].append(i)
            else:
                temp2[a] = [i]
            temp1[b] = temp1.get(b, 0) + 1
            if temp1[b] > 1: 
                temp2[b].append(i)
            else:
                temp2[b] = [i]
        first = min(temp1, key=temp1.get)

        # Perform a depth-first search from the first element.
        res.append(first)
        for _ in range(n-1):
            temp_id = temp2[res[-1]][0]
            a, b = adjacentPairs[temp_id]
            if a == res[-1]:
                res.append(b)
                temp2[b].remove(temp_id)
            else:
                res.append(a)
                temp2[a].remove(temp_id)
        
        return res

备选方案(超时未通过)

class Solution:
    def __init__(self):
        self.res = []

    def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
        # Find the first element of nums - it will only appear once in adjacentPairs.
        temp = {}
        for a, b in adjacentPairs:
            temp[a] = temp.get(a, 0) + 1
            temp[b] = temp.get(b, 0) + 1
        first = min(temp, key=temp.get)

        # Perform a depth-first search from the first element.
        self.res.append(first)
        self.helper(adjacentPairs)
        return self.res

    def helper(self, adjacentPairs):
        for pair in adjacentPairs:
            if self.res[-1] in pair:
                if pair[0] == self.res[-1]:
                    self.res.append(pair[1])
                else:
                    self.res.append(pair[0])
                if len(adjacentPairs) > 1:
                    adjacentPairs.remove(pair)
                    return self.helper(adjacentPairs)
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-26 12:18:24  更:2021-07-26 12:19:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/24 21:48:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码