Skip to content

Commit 7a8906b

Browse files
author
Shuo
committed
A: Destination City
1 parent cb477f3 commit 7a8906b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package problem1436
2+
3+
func destCity(paths [][]string) string {
4+
m := make(map[string]bool)
5+
for _, path := range paths {
6+
m[path[0]] = true
7+
}
8+
for _, path := range paths {
9+
if !m[path[1]] {
10+
return path[1]
11+
}
12+
}
13+
return ""
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package problem1436
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in [][]string
7+
want string
8+
}
9+
10+
func TestDestCity(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: [][]string{
14+
{"London", "New York"},
15+
{"New York", "Lima"},
16+
{"Lima", "Sao Paulo"},
17+
},
18+
want: "Sao Paulo",
19+
},
20+
{
21+
in: [][]string{
22+
{"B", "C"},
23+
{"D", "B"},
24+
{"C", "A"},
25+
},
26+
want: "A",
27+
},
28+
{
29+
in: [][]string{
30+
{"A", "Z"},
31+
},
32+
want: "Z",
33+
},
34+
}
35+
for _, tt := range tests {
36+
got := destCity(tt.in)
37+
if got != tt.want {
38+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)