Skip to content

Commit b9a06c1

Browse files
committed
feat: add solutions to lc problem: No.1743.Restore the Array From Adjacent Pairs
1 parent ee32cb5 commit b9a06c1

File tree

5 files changed

+273
-2
lines changed

5 files changed

+273
-2
lines changed

solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md

+93-1
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,114 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
从度为一的点开始遍历图
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

6668
<!-- 这里可写当前语言的特殊实现逻辑 -->
6769

6870
```python
69-
71+
class Solution:
72+
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
73+
graph = collections.defaultdict(list)
74+
for pair in adjacentPairs:
75+
graph[pair[0]].append(pair[1])
76+
graph[pair[1]].append(pair[0])
77+
ans = []
78+
vis = set()
79+
80+
def dfs(idx):
81+
if idx in vis:
82+
return
83+
vis.add(idx)
84+
ans.append(idx)
85+
for nxt in graph[idx]:
86+
dfs(nxt)
87+
88+
start = -1
89+
for idx, adj in graph.items():
90+
if len(adj) == 1:
91+
start = idx
92+
break
93+
94+
dfs(start)
95+
return ans
7096
```
7197

7298
### **Java**
7399

74100
<!-- 这里可写当前语言的特殊实现逻辑 -->
75101

76102
```java
103+
class Solution {
104+
public int[] restoreArray(int[][] adjacentPairs) {
105+
Map<Integer, List<Integer>> graph = new HashMap<>();
106+
for (int[] pair : adjacentPairs) {
107+
graph.putIfAbsent(pair[0], new ArrayList<>());
108+
graph.putIfAbsent(pair[1], new ArrayList<>());
109+
graph.get(pair[0]).add(pair[1]);
110+
graph.get(pair[1]).add(pair[0]);
111+
}
112+
List<Integer> ans = new ArrayList<>();
113+
Set<Integer> vis = new HashSet<>();
114+
int start = -1;
115+
for (Map.Entry<Integer, List<Integer>> entry : graph.entrySet()) {
116+
if (entry.getValue().size() == 1) {
117+
start = entry.getKey();
118+
break;
119+
}
120+
}
121+
dfs(graph, ans, vis, start);
122+
return ans.stream().mapToInt(Integer::valueOf).toArray();
123+
}
124+
125+
private void dfs(Map<Integer, List<Integer>> graph, List<Integer> ans, Set<Integer> vis, int idx) {
126+
if (vis.contains(idx)) {
127+
return;
128+
}
129+
vis.add(idx);
130+
ans.add(idx);
131+
for (Integer next : graph.get(idx)) {
132+
dfs(graph, ans, vis, next);
133+
}
134+
}
135+
}
136+
```
77137

138+
### **Go**
139+
140+
```go
141+
func restoreArray(adjacentPairs [][]int) []int {
142+
graph := make(map[int][]int)
143+
for _, pair := range adjacentPairs {
144+
graph[pair[0]] = append(graph[pair[0]], pair[1])
145+
graph[pair[1]] = append(graph[pair[1]], pair[0])
146+
}
147+
ans := make([]int, 0)
148+
vis := make(map[int]bool)
149+
var start int
150+
for idx, adj := range graph {
151+
if len(adj) == 1 {
152+
start = idx
153+
break
154+
}
155+
}
156+
dfs(graph, &ans, vis, start)
157+
return ans
158+
}
159+
160+
func dfs(graph map[int][]int, ans *[]int, vis map[int]bool, idx int) {
161+
if vis[idx] {
162+
return
163+
}
164+
vis[idx] = true
165+
*ans = append(*ans, idx)
166+
for _, next := range graph[idx] {
167+
dfs(graph, ans, vis, next)
168+
}
169+
}
78170
```
79171

80172
### **...**

solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md

+93-1
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,110 @@ Another solution is [-3,1,4,-2], which would also be accepted.
5353

5454
## Solutions
5555

56+
Traverse the graph from the point where the degree is one.
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

6062
```python
61-
63+
class Solution:
64+
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
65+
graph = collections.defaultdict(list)
66+
for pair in adjacentPairs:
67+
graph[pair[0]].append(pair[1])
68+
graph[pair[1]].append(pair[0])
69+
ans = []
70+
vis = set()
71+
72+
def dfs(idx):
73+
if idx in vis:
74+
return
75+
vis.add(idx)
76+
ans.append(idx)
77+
for nxt in graph[idx]:
78+
dfs(nxt)
79+
80+
start = -1
81+
for idx, adj in graph.items():
82+
if len(adj) == 1:
83+
start = idx
84+
break
85+
86+
dfs(start)
87+
return ans
6288
```
6389

6490
### **Java**
6591

6692
```java
93+
class Solution {
94+
public int[] restoreArray(int[][] adjacentPairs) {
95+
Map<Integer, List<Integer>> graph = new HashMap<>();
96+
for (int[] pair : adjacentPairs) {
97+
graph.putIfAbsent(pair[0], new ArrayList<>());
98+
graph.putIfAbsent(pair[1], new ArrayList<>());
99+
graph.get(pair[0]).add(pair[1]);
100+
graph.get(pair[1]).add(pair[0]);
101+
}
102+
List<Integer> ans = new ArrayList<>();
103+
Set<Integer> vis = new HashSet<>();
104+
int start = -1;
105+
for (Map.Entry<Integer, List<Integer>> entry : graph.entrySet()) {
106+
if (entry.getValue().size() == 1) {
107+
start = entry.getKey();
108+
break;
109+
}
110+
}
111+
dfs(graph, ans, vis, start);
112+
return ans.stream().mapToInt(Integer::valueOf).toArray();
113+
}
114+
115+
private void dfs(Map<Integer, List<Integer>> graph, List<Integer> ans, Set<Integer> vis, int idx) {
116+
if (vis.contains(idx)) {
117+
return;
118+
}
119+
vis.add(idx);
120+
ans.add(idx);
121+
for (Integer next : graph.get(idx)) {
122+
dfs(graph, ans, vis, next);
123+
}
124+
}
125+
}
126+
```
67127

128+
### **Go**
129+
130+
```go
131+
func restoreArray(adjacentPairs [][]int) []int {
132+
graph := make(map[int][]int)
133+
for _, pair := range adjacentPairs {
134+
graph[pair[0]] = append(graph[pair[0]], pair[1])
135+
graph[pair[1]] = append(graph[pair[1]], pair[0])
136+
}
137+
ans := make([]int, 0)
138+
vis := make(map[int]bool)
139+
var start int
140+
for idx, adj := range graph {
141+
if len(adj) == 1 {
142+
start = idx
143+
break
144+
}
145+
}
146+
dfs(graph, &ans, vis, start)
147+
return ans
148+
}
149+
150+
func dfs(graph map[int][]int, ans *[]int, vis map[int]bool, idx int) {
151+
if vis[idx] {
152+
return
153+
}
154+
vis[idx] = true
155+
*ans = append(*ans, idx)
156+
for _, next := range graph[idx] {
157+
dfs(graph, ans, vis, next)
158+
}
159+
}
68160
```
69161

70162
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func restoreArray(adjacentPairs [][]int) []int {
2+
graph := make(map[int][]int)
3+
for _, pair := range adjacentPairs {
4+
graph[pair[0]] = append(graph[pair[0]], pair[1])
5+
graph[pair[1]] = append(graph[pair[1]], pair[0])
6+
}
7+
ans := make([]int, 0)
8+
vis := make(map[int]bool)
9+
var start int
10+
for idx, adj := range graph {
11+
if len(adj) == 1 {
12+
start = idx
13+
break
14+
}
15+
}
16+
dfs(graph, &ans, vis, start)
17+
return ans
18+
}
19+
20+
func dfs(graph map[int][]int, ans *[]int, vis map[int]bool, idx int) {
21+
if vis[idx] {
22+
return
23+
}
24+
vis[idx] = true
25+
*ans = append(*ans, idx)
26+
for _, next := range graph[idx] {
27+
dfs(graph, ans, vis, next)
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int[] restoreArray(int[][] adjacentPairs) {
3+
Map<Integer, List<Integer>> graph = new HashMap<>();
4+
for (int[] pair : adjacentPairs) {
5+
graph.putIfAbsent(pair[0], new ArrayList<>());
6+
graph.putIfAbsent(pair[1], new ArrayList<>());
7+
graph.get(pair[0]).add(pair[1]);
8+
graph.get(pair[1]).add(pair[0]);
9+
}
10+
List<Integer> ans = new ArrayList<>();
11+
Set<Integer> vis = new HashSet<>();
12+
int start = -1;
13+
for (Map.Entry<Integer, List<Integer>> entry : graph.entrySet()) {
14+
if (entry.getValue().size() == 1) {
15+
start = entry.getKey();
16+
break;
17+
}
18+
}
19+
dfs(graph, ans, vis, start);
20+
return ans.stream().mapToInt(Integer::valueOf).toArray();
21+
}
22+
23+
private void dfs(Map<Integer, List<Integer>> graph, List<Integer> ans, Set<Integer> vis, int idx) {
24+
if (vis.contains(idx)) {
25+
return;
26+
}
27+
vis.add(idx);
28+
ans.add(idx);
29+
for (Integer next : graph.get(idx)) {
30+
dfs(graph, ans, vis, next);
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
3+
graph = collections.defaultdict(list)
4+
for pair in adjacentPairs:
5+
graph[pair[0]].append(pair[1])
6+
graph[pair[1]].append(pair[0])
7+
ans = []
8+
vis = set()
9+
10+
def dfs(idx):
11+
if idx in vis:
12+
return
13+
vis.add(idx)
14+
ans.append(idx)
15+
for nxt in graph[idx]:
16+
dfs(nxt)
17+
18+
start = -1
19+
for idx, adj in graph.items():
20+
if len(adj) == 1:
21+
start = idx
22+
break
23+
24+
dfs(start)
25+
return ans

0 commit comments

Comments
 (0)