{"id":8049,"date":"2021-01-30T20:53:18","date_gmt":"2021-01-31T04:53:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8049"},"modified":"2021-01-30T20:53:47","modified_gmt":"2021-01-31T04:53:47","slug":"leetcode-1743-restore-the-array-from-adjacent-pairs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1743-restore-the-array-from-adjacent-pairs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1743. Restore the Array From Adjacent Pairs"},"content":{"rendered":"\n<p>There is an integer array&nbsp;<code>nums<\/code>&nbsp;that consists of&nbsp;<code>n<\/code>&nbsp;<strong>unique&nbsp;<\/strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>You are given a 2D integer array&nbsp;<code>adjacentPairs<\/code>&nbsp;of size&nbsp;<code>n - 1<\/code>&nbsp;where each&nbsp;<code>adjacentPairs[i] = [u<sub>i<\/sub>, v<sub>i<\/sub>]<\/code>&nbsp;indicates that the elements&nbsp;<code>u<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>v<sub>i<\/sub><\/code>&nbsp;are adjacent in&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>It is guaranteed that every adjacent pair of elements&nbsp;<code>nums[i]<\/code>&nbsp;and&nbsp;<code>nums[i+1]<\/code>&nbsp;will exist in&nbsp;<code>adjacentPairs<\/code>, either as&nbsp;<code>[nums[i], nums[i+1]]<\/code>&nbsp;or&nbsp;<code>[nums[i+1], nums[i]]<\/code>. The pairs can appear&nbsp;<strong>in any order<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the original array&nbsp;<\/em><code>nums<\/code><em>. If there are multiple solutions, return&nbsp;<strong>any of them<\/strong><\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> adjacentPairs = [[2,1],[3,4],[3,2]]\n<strong>Output:<\/strong> [1,2,3,4]\n<strong>Explanation:<\/strong> This array has all its adjacent pairs in adjacentPairs.\nNotice that adjacentPairs[i] may not be in left-to-right order.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> adjacentPairs = [[4,-2],[1,4],[-3,1]]\n<strong>Output:<\/strong> [-2,4,1,-3]\n<strong>Explanation:<\/strong> There can be negative numbers.\nAnother solution is [-3,1,4,-2], which would also be accepted.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> adjacentPairs = [[100000,-100000]]\n<strong>Output:<\/strong> [100000,-100000]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>nums.length == n<\/code><\/li><li><code>adjacentPairs.length == n - 1<\/code><\/li><li><code>adjacentPairs[i].length == 2<\/code><\/li><li><code>2 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= nums[i], u<sub>i<\/sub>, v<sub>i<\/sub>&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><li>There exists some&nbsp;<code>nums<\/code>&nbsp;that has&nbsp;<code>adjacentPairs<\/code>&nbsp;as its pairs.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable <\/strong><\/h2>\n\n\n\n<p>Reverse thinking! For a given input array, e.g. <br>[1, 2, 3, 4, 5]<br>it&#8217;s adjacent pairs are [1,2] , [2,3], [3,4], [4,5]<br>all numbers appeared exactly twice except 1 and 5, since they are on the boundary. <br>We just need to find the head or tail of the input array, and construct the rest of the array in order.<\/p>\n\n\n\n<p>Time complexity:O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {\n    const int n = adjacentPairs.size() + 1;\n    unordered_map<int, vector<int>> g;\n    for (const auto& p : adjacentPairs) {\n      g[p[0]].push_back(p[1]);\n      g[p[1]].push_back(p[0]);\n    }\n    \n    vector<int> ans(n);\n    for (const auto& [u, vs] : g)\n      if (vs.size() == 1) {\n        ans[0] = u;\n        ans[1] = vs[0];\n        break;\n      }\n        \n    for (int i = 2; i < n; ++i) {\n      const auto&#038; vs = g[ans[i - 1]];\n      ans[i] = vs[0] == ans[i - 2] ? vs[1] : vs[0];      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an integer array&nbsp;nums&nbsp;that consists of&nbsp;n&nbsp;unique&nbsp;elements, but you have forgotten it. However, you do remember every pair of adjacent elements in&nbsp;nums. You are given&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[20,578,82,177],"class_list":["post-8049","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-construction","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8049"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8049\/revisions"}],"predecessor-version":[{"id":8051,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8049\/revisions\/8051"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}