From 867d19cde1e1abafb1387837c53671ee92cf61c6 Mon Sep 17 00:00:00 2001 From: Pandurang Lad Date: Fri, 10 Nov 2023 19:16:08 +0530 Subject: [PATCH 1/4] feat: add solution.cs to lc problems: No.1743 --- .../Solution.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 solution/1700-1799/1743.Restore the Array From Adjacent Pairs/Solution.cs diff --git a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/Solution.cs b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/Solution.cs new file mode 100644 index 0000000000000..b2763f7f20b93 --- /dev/null +++ b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/Solution.cs @@ -0,0 +1,35 @@ +public class Solution { + public int[] RestoreArray(int[][] adjacentPairs) { + int n = adjacentPairs.Length + 1; + Dictionary> g = new Dictionary>(); + + foreach (int[] e in adjacentPairs) { + int a = e[0], b = e[1]; + if (!g.ContainsKey(a)) { + g[a] = new List(); + } + if (!g.ContainsKey(b)) { + g[b] = new List(); + } + g[a].Add(b); + g[b].Add(a); + } + + int[] ans = new int[n]; + + foreach (var entry in g) { + if (entry.Value.Count == 1) { + ans[0] = entry.Key; + ans[1] = entry.Value[0]; + break; + } + } + + for (int i = 2; i < n; ++i) { + List v = g[ans[i - 1]]; + ans[i] = v[1] == ans[i - 2] ? v[0] : v[1]; + } + + return ans; + } +} From 14062467e0da7615bbd45e85fafdf0683da4c97a Mon Sep 17 00:00:00 2001 From: Pandurang Lad Date: Fri, 10 Nov 2023 19:18:50 +0530 Subject: [PATCH 2/4] Update README_EN.md to lc problems: No.1743 --- .../README_EN.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md index ab09e33d0166e..536537653407e 100644 --- a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md +++ b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md @@ -278,6 +278,46 @@ func restoreArray(adjacentPairs [][]int) []int { } ``` +### **C#** + +```cs +public class Solution { + public int[] RestoreArray(int[][] adjacentPairs) { + int n = adjacentPairs.Length + 1; + Dictionary> g = new Dictionary>(); + + foreach (int[] e in adjacentPairs) { + int a = e[0], b = e[1]; + if (!g.ContainsKey(a)) { + g[a] = new List(); + } + if (!g.ContainsKey(b)) { + g[b] = new List(); + } + g[a].Add(b); + g[b].Add(a); + } + + int[] ans = new int[n]; + + foreach (var entry in g) { + if (entry.Value.Count == 1) { + ans[0] = entry.Key; + ans[1] = entry.Value[0]; + break; + } + } + + for (int i = 2; i < n; ++i) { + List v = g[ans[i - 1]]; + ans[i] = v[1] == ans[i - 2] ? v[0] : v[1]; + } + + return ans; + } +} +``` + ### **...** ``` From 5fc52854bffd6410b956b23b64447973e1bd17c2 Mon Sep 17 00:00:00 2001 From: dev-mauli Date: Fri, 10 Nov 2023 13:49:53 +0000 Subject: [PATCH 3/4] style: format code and docs with prettier --- .../README_EN.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md index 536537653407e..b679c3e65bf46 100644 --- a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md +++ b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md @@ -285,7 +285,7 @@ public class Solution { public int[] RestoreArray(int[][] adjacentPairs) { int n = adjacentPairs.Length + 1; Dictionary> g = new Dictionary>(); - + foreach (int[] e in adjacentPairs) { int a = e[0], b = e[1]; if (!g.ContainsKey(a)) { @@ -297,9 +297,9 @@ public class Solution { g[a].Add(b); g[b].Add(a); } - + int[] ans = new int[n]; - + foreach (var entry in g) { if (entry.Value.Count == 1) { ans[0] = entry.Key; @@ -307,12 +307,12 @@ public class Solution { break; } } - + for (int i = 2; i < n; ++i) { List v = g[ans[i - 1]]; ans[i] = v[1] == ans[i - 2] ? v[0] : v[1]; } - + return ans; } } From d0a13bce892af0a8058157a78fce78bbdc2437ff Mon Sep 17 00:00:00 2001 From: Pandurang Lad Date: Fri, 10 Nov 2023 19:20:45 +0530 Subject: [PATCH 4/4] Update README.md to lc problems: No.1743 --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md index 698b337a25015..6d02f3131644c 100644 --- a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md +++ b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md @@ -292,6 +292,46 @@ func restoreArray(adjacentPairs [][]int) []int { } ``` +### **C#** + +```cs +public class Solution { + public int[] RestoreArray(int[][] adjacentPairs) { + int n = adjacentPairs.Length + 1; + Dictionary> g = new Dictionary>(); + + foreach (int[] e in adjacentPairs) { + int a = e[0], b = e[1]; + if (!g.ContainsKey(a)) { + g[a] = new List(); + } + if (!g.ContainsKey(b)) { + g[b] = new List(); + } + g[a].Add(b); + g[b].Add(a); + } + + int[] ans = new int[n]; + + foreach (var entry in g) { + if (entry.Value.Count == 1) { + ans[0] = entry.Key; + ans[1] = entry.Value[0]; + break; + } + } + + for (int i = 2; i < n; ++i) { + List v = g[ans[i - 1]]; + ans[i] = v[1] == ans[i - 2] ? v[0] : v[1]; + } + + return ans; + } +} +``` + ### **...** ```