Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54ac9e5

Browse files
feat: add cs solution to lc problem: No.1743 (doocs#1952)
1 parent bcb9d14 commit 54ac9e5

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,46 @@ func restoreArray(adjacentPairs [][]int) []int {
292292
}
293293
```
294294

295+
### **C#**
296+
297+
```cs
298+
public class Solution {
299+
public int[] RestoreArray(int[][] adjacentPairs) {
300+
int n = adjacentPairs.Length + 1;
301+
Dictionary<int, List<int>> g = new Dictionary<int, List<int>>();
302+
303+
foreach (int[] e in adjacentPairs) {
304+
int a = e[0], b = e[1];
305+
if (!g.ContainsKey(a)) {
306+
g[a] = new List<int>();
307+
}
308+
if (!g.ContainsKey(b)) {
309+
g[b] = new List<int>();
310+
}
311+
g[a].Add(b);
312+
g[b].Add(a);
313+
}
314+
315+
int[] ans = new int[n];
316+
317+
foreach (var entry in g) {
318+
if (entry.Value.Count == 1) {
319+
ans[0] = entry.Key;
320+
ans[1] = entry.Value[0];
321+
break;
322+
}
323+
}
324+
325+
for (int i = 2; i < n; ++i) {
326+
List<int> v = g[ans[i - 1]];
327+
ans[i] = v[1] == ans[i - 2] ? v[0] : v[1];
328+
}
329+
330+
return ans;
331+
}
332+
}
333+
```
334+
295335
### **...**
296336

297337
```

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,46 @@ func restoreArray(adjacentPairs [][]int) []int {
278278
}
279279
```
280280

281+
### **C#**
282+
283+
```cs
284+
public class Solution {
285+
public int[] RestoreArray(int[][] adjacentPairs) {
286+
int n = adjacentPairs.Length + 1;
287+
Dictionary<int, List<int>> g = new Dictionary<int, List<int>>();
288+
289+
foreach (int[] e in adjacentPairs) {
290+
int a = e[0], b = e[1];
291+
if (!g.ContainsKey(a)) {
292+
g[a] = new List<int>();
293+
}
294+
if (!g.ContainsKey(b)) {
295+
g[b] = new List<int>();
296+
}
297+
g[a].Add(b);
298+
g[b].Add(a);
299+
}
300+
301+
int[] ans = new int[n];
302+
303+
foreach (var entry in g) {
304+
if (entry.Value.Count == 1) {
305+
ans[0] = entry.Key;
306+
ans[1] = entry.Value[0];
307+
break;
308+
}
309+
}
310+
311+
for (int i = 2; i < n; ++i) {
312+
List<int> v = g[ans[i - 1]];
313+
ans[i] = v[1] == ans[i - 2] ? v[0] : v[1];
314+
}
315+
316+
return ans;
317+
}
318+
}
319+
```
320+
281321
### **...**
282322

283323
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class Solution {
2+
public int[] RestoreArray(int[][] adjacentPairs) {
3+
int n = adjacentPairs.Length + 1;
4+
Dictionary<int, List<int>> g = new Dictionary<int, List<int>>();
5+
6+
foreach (int[] e in adjacentPairs) {
7+
int a = e[0], b = e[1];
8+
if (!g.ContainsKey(a)) {
9+
g[a] = new List<int>();
10+
}
11+
if (!g.ContainsKey(b)) {
12+
g[b] = new List<int>();
13+
}
14+
g[a].Add(b);
15+
g[b].Add(a);
16+
}
17+
18+
int[] ans = new int[n];
19+
20+
foreach (var entry in g) {
21+
if (entry.Value.Count == 1) {
22+
ans[0] = entry.Key;
23+
ans[1] = entry.Value[0];
24+
break;
25+
}
26+
}
27+
28+
for (int i = 2; i < n; ++i) {
29+
List<int> v = g[ans[i - 1]];
30+
ans[i] = v[1] == ans[i - 2] ? v[0] : v[1];
31+
}
32+
33+
return ans;
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /