File tree Expand file tree Collapse file tree 6 files changed +150
-2
lines changed
solution/1400-1499/1436.Destination City Expand file tree Collapse file tree 6 files changed +150
-2
lines changed Original file line number Diff line number Diff line change 61
61
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
62
63
63
``` 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
65
71
```
66
72
67
73
### ** Java**
68
74
69
75
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
76
71
77
``` 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
+ ```
72
107
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
+ }
73
125
```
74
126
75
127
### ** ...**
Original file line number Diff line number Diff line change @@ -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
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments