Skip to content

Commit 93a3ab3

Browse files
committed
feat: add solutions to lc problem: No.1436.Destination City
1 parent 8d4bf84 commit 93a3ab3

File tree

6 files changed

+150
-2
lines changed

6 files changed

+150
-2
lines changed

solution/1400-1499/1436.Destination City/README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,67 @@
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```python
64-
64+
class Solution:
65+
def destCity(self, paths: List[List[str]]) -> str:
66+
mp = {a: b for a, b in paths}
67+
a = paths[0][0]
68+
while mp.get(a):
69+
a = mp[a]
70+
return a
6571
```
6672

6773
### **Java**
6874

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

7177
```java
78+
class Solution {
79+
public String destCity(List<List<String>> paths) {
80+
Map<String, String> mp = new HashMap<>();
81+
for (List<String> path : paths) {
82+
mp.put(path.get(0), path.get(1));
83+
}
84+
String a = paths.get(0).get(0);
85+
while (mp.get(a) != null) {
86+
a = mp.get(a);
87+
}
88+
return a;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
string destCity(vector<vector<string>>& paths) {
99+
unordered_map<string, string> mp;
100+
for (auto& path : paths) mp[path[0]] = path[1];
101+
string a = paths[0][0];
102+
while (mp.find(a) != mp.end()) a = mp[a];
103+
return a;
104+
}
105+
};
106+
```
72107
108+
### **Go**
109+
110+
```go
111+
func destCity(paths [][]string) string {
112+
mp := make(map[string]string)
113+
for _, path := range paths {
114+
mp[path[0]] = path[1]
115+
}
116+
a := paths[0][0]
117+
for true {
118+
if _, ok := mp[a]; !ok {
119+
return a
120+
}
121+
a = mp[a]
122+
}
123+
return ""
124+
}
73125
```
74126

75127
### **...**

solution/1400-1499/1436.Destination City/README_EN.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,65 @@ Clearly the destination city is &quot;A&quot;.
5656
### **Python3**
5757

5858
```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
6066
```
6167

6268
### **Java**
6369

6470
```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+
```
65100
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+
}
66118
```
67119

68120
### **...**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
string destCity(vector<vector<string>>& paths) {
4+
unordered_map<string, string> mp;
5+
for (auto& path : paths) mp[path[0]] = path[1];
6+
string a = paths[0][0];
7+
while (mp.find(a) != mp.end()) a = mp[a];
8+
return a;
9+
}
10+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func destCity(paths [][]string) string {
2+
mp := make(map[string]string)
3+
for _, path := range paths {
4+
mp[path[0]] = path[1]
5+
}
6+
a := paths[0][0]
7+
for true {
8+
if _, ok := mp[a]; !ok {
9+
return a
10+
}
11+
a = mp[a]
12+
}
13+
return ""
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public String destCity(List<List<String>> paths) {
3+
Map<String, String> mp = new HashMap<>();
4+
for (List<String> path : paths) {
5+
mp.put(path.get(0), path.get(1));
6+
}
7+
String a = paths.get(0).get(0);
8+
while (mp.get(a) != null) {
9+
a = mp.get(a);
10+
}
11+
return a;
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def destCity(self, paths: List[List[str]]) -> str:
3+
mp = {a: b for a, b in paths}
4+
a = paths[0][0]
5+
while mp.get(a):
6+
a = mp[a]
7+
return a

0 commit comments

Comments
 (0)