1743. Restore the Array From Adjacent Pairs Medium
1/**
2 * [1743] Restore the Array From Adjacent Pairs
3 *
4 * There is an integer array nums that consists of n unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums.
5 * You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums.
6 * It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear in any order.
7 * Return the original array nums. If there are multiple solutions, return any of them.
8 *
9 * Example 1:
10 *
11 * Input: adjacentPairs = [[2,1],[3,4],[3,2]]
12 * Output: [1,2,3,4]
13 * Explanation: This array has all its adjacent pairs in adjacentPairs.
14 * Notice that adjacentPairs[i] may not be in left-to-right order.
15 *
16 * Example 2:
17 *
18 * Input: adjacentPairs = [[4,-2],[1,4],[-3,1]]
19 * Output: [-2,4,1,-3]
20 * Explanation: There can be negative numbers.
21 * Another solution is [-3,1,4,-2], which would also be accepted.
22 *
23 * Example 3:
24 *
25 * Input: adjacentPairs = [[100000,-100000]]
26 * Output: [100000,-100000]
27 *
28 *
29 * Constraints:
30 *
31 * nums.length == n
32 * adjacentPairs.length == n - 1
33 * adjacentPairs[i].length == 2
34 * 2 <= n <= 10^5
35 * -10^5 <= nums[i], ui, vi <= 10^5
36 * There exists some nums that has adjacentPairs as its pairs.
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/
42// discuss: https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn restore_array(adjacent_pairs: Vec<Vec<i32>>) -> Vec<i32> {
48 vec![]
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_1743() {
60 }
61}
62
Back
© 2025 bowen.ge All Rights Reserved.