File tree Expand file tree Collapse file tree 3 files changed +115
-0
lines changed
solution/1700-1799/1743.Restore the Array From Adjacent Pairs Expand file tree Collapse file tree 3 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -292,6 +292,46 @@ func restoreArray(adjacentPairs [][]int) []int {
292
292
}
293
293
```
294
294
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
+
295
335
### ** ...**
296
336
297
337
```
Original file line number Diff line number Diff line change @@ -278,6 +278,46 @@ func restoreArray(adjacentPairs [][]int) []int {
278
278
}
279
279
```
280
280
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
+
281
321
### ** ...**
282
322
283
323
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments