@@ -56,13 +56,65 @@ Clearly the destination city is "A".
56
56
### ** Python3**
57
57
58
58
``` python
59
-
59
+ class Solution :
60
+ def destCity (self , paths : List[List[str ]]) -> str :
61
+ mp = {a: b for a, b in paths}
62
+ a = paths[0 ][0 ]
63
+ while mp.get(a):
64
+ a = mp[a]
65
+ return a
60
66
```
61
67
62
68
### ** Java**
63
69
64
70
``` java
71
+ class Solution {
72
+ public String destCity (List<List<String > > paths ) {
73
+ Map<String , String > mp = new HashMap<> ();
74
+ for (List<String > path : paths) {
75
+ mp. put(path. get(0 ), path. get(1 ));
76
+ }
77
+ String a = paths. get(0 ). get(0 );
78
+ while (mp. get(a) != null ) {
79
+ a = mp. get(a);
80
+ }
81
+ return a;
82
+ }
83
+ }
84
+ ```
85
+
86
+ ### ** C++**
87
+
88
+ ``` cpp
89
+ class Solution {
90
+ public:
91
+ string destCity(vector<vector<string >>& paths) {
92
+ unordered_map<string, string> mp;
93
+ for (auto& path : paths) mp[ path[ 0]] = path[ 1] ;
94
+ string a = paths[ 0] [ 0 ] ;
95
+ while (mp.find(a) != mp.end()) a = mp[ a] ;
96
+ return a;
97
+ }
98
+ };
99
+ ```
65
100
101
+ ### **Go**
102
+
103
+ ```go
104
+ func destCity(paths [][]string) string {
105
+ mp := make(map[string]string)
106
+ for _, path := range paths {
107
+ mp[path[0]] = path[1]
108
+ }
109
+ a := paths[0][0]
110
+ for true {
111
+ if _, ok := mp[a]; !ok {
112
+ return a
113
+ }
114
+ a = mp[a]
115
+ }
116
+ return ""
117
+ }
66
118
```
67
119
68
120
### ** ...**
0 commit comments