1743:从相邻元素对还原数组

This commit is contained in:
huangge1199@hotmail.com 2021-07-25 20:00:14 +08:00
parent 091b1e6666
commit 62fce5b6e4
3 changed files with 145 additions and 1 deletions

View File

@ -0,0 +1,96 @@
//存在一个由 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 如果存在多种解答返回 其中任意一个 即可
//
//
//
// 示例 1
//
//
//输入adjacentPairs = [[2,1],[3,4],[3,2]]
//输出[1,2,3,4]
//解释数组的所有相邻元素对都在 adjacentPairs
//特别要注意的是adjacentPairs[i] 只表示两个元素相邻并不保证其 - 顺序
//
//
// 示例 2
//
//
//输入adjacentPairs = [[4,-2],[1,4],[-3,1]]
//输出[-2,4,1,-3]
//解释数组中可能存在负数
//另一种解答是 [-3,1,4,-2] 也会被视作正确答案
//
//
// 示例 3
//
//
//输入adjacentPairs = [[100000,-100000]]
//输出[100000,-100000]
//
//
//
//
// 提示
//
//
// nums.length == n
// adjacentPairs.length == n - 1
// adjacentPairs[i].length == 2
// 2 <= n <= 105
// -105 <= nums[i], ui, vi <= 105
// 题目数据保证存在一些以 adjacentPairs 作为元素对的数组 nums
//
// Related Topics 数组 哈希表
// 👍 58 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//1743:从相邻元素对还原数组
class RestoreTheArrayFromAdjacentPairs {
public static void main(String[] args) {
//测试代码
Solution solution = new RestoreTheArrayFromAdjacentPairs().new Solution();
solution.restoreArray(new int[][]{{2, 1}, {3, 4}, {3, 2}});
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] restoreArray(int[][] adjacentPairs) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int[] adjacentPair : adjacentPairs) {
map.putIfAbsent(adjacentPair[0], new ArrayList<Integer>());
map.putIfAbsent(adjacentPair[1], new ArrayList<Integer>());
map.get(adjacentPair[0]).add(adjacentPair[1]);
map.get(adjacentPair[1]).add(adjacentPair[0]);
}
int[] arr = new int[adjacentPairs.length + 1];
for (int key : map.keySet()) {
if (map.get(key).size() == 1) {
arr[0] = key;
break;
}
}
arr[1] = map.get(arr[0]).get(0);
for (int i = 2; i < adjacentPairs.length + 1; i++) {
List<Integer> list = map.get(arr[i - 1]);
arr[i] = arr[i - 2] == list.get(0) ? list.get(1) : list.get(0);
}
return arr;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,48 @@
<p>存在一个由 <code>n</code> 个不同元素组成的整数数组 <code>nums</code> ,但你已经记不清具体内容。好在你还记得 <code>nums</code> 中的每一对相邻元素。</p>
<p>给你一个二维整数数组 <code>adjacentPairs</code> ,大小为 <code>n - 1</code> ,其中每个 <code>adjacentPairs[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示元素 <code>u<sub>i</sub></code><code>v<sub>i</sub></code><code>nums</code> 中相邻。</p>
<p>题目数据保证所有由元素 <code>nums[i]</code><code>nums[i+1]</code> 组成的相邻元素对都存在于 <code>adjacentPairs</code> 中,存在形式可能是 <code>[nums[i], nums[i+1]]</code> ,也可能是 <code>[nums[i+1], nums[i]]</code> 。这些相邻元素对可以 <strong>按任意顺序</strong> 出现。</p>
<p>返回 <strong>原始数组</strong><em> </em><code>nums</code><em> </em>。如果存在多种解答,返回 <strong>其中任意一个</strong> 即可。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>adjacentPairs = [[2,1],[3,4],[3,2]]
<strong>输出:</strong>[1,2,3,4]
<strong>解释:</strong>数组的所有相邻元素对都在 adjacentPairs 中。
特别要注意的是adjacentPairs[i] 只表示两个元素相邻,并不保证其 左-右 顺序。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>adjacentPairs = [[4,-2],[1,4],[-3,1]]
<strong>输出:</strong>[-2,4,1,-3]
<strong>解释:</strong>数组中可能存在负数。
另一种解答是 [-3,1,4,-2] ,也会被视作正确答案。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>adjacentPairs = [[100000,-100000]]
<strong>输出:</strong>[100000,-100000]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<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 <= n <= 10<sup>5</sup></code></li>
<li><code>-10<sup>5</sup> <= nums[i], u<sub>i</sub>, v<sub>i</sub> <= 10<sup>5</sup></code></li>
<li>题目数据保证存在一些以 <code>adjacentPairs</code> 作为元素对的数组 <code>nums</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li></div></div>\n<div><li>👍 58</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long