Skip to content

Commit

Permalink
脑子不是很好使了,转不动了是不是年纪大了啊
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerxiong committed Jul 25, 2021
1 parent 8bd95d7 commit 7a67e8a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 1743.从相邻元素对还原数组.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* @lc app=leetcode.cn id=1743 lang=golang
*
* [1743] 从相邻元素对还原数组
*/

// @lc code=start
func restoreArray(adjacentPairs [][]int) []int {
var (
m = make(map[int][]int)
n = len(adjacentPairs) + 1
)

for _, nums := range adjacentPairs {
v := nums[0]
w := nums[1]
m[v] = append(m[v], w)
m[w] = append(m[w], v)
}

ans := make([]int, n)
for k, n := range m {
if len(n) == 1 {
ans[0] = k
break
}
}

ans[1] = m[ans[0]][0]

for i := 2; i < n; i++ {
adj := m[ans[i-1]]
if ans[i-2] == adj[0] {
ans[i] = adj[1]
} else {
ans[i] = adj[0]
}
}
return ans
}

// @lc code=end

0 comments on commit 7a67e8a

Please sign in to comment.