Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7e18c10

Browse files
authored
Merge pull requestneetcode-gh#1935 from kkr2/feat/0332-reconstruct-itenerary.go
Create 0332-reconstruct-itenerary.go
2 parents13cf381 +4edd518 commit7e18c10

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎go/0332-reconstruct-itenerary.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
funcfindItinerary(tickets [][]string) []string {
2+
3+
adjMap:=make(map[string][]string)
4+
5+
fori:=0;i<len(tickets);i++ {
6+
adjMap[tickets[i][0]]=append(adjMap[tickets[i][0]],tickets[i][1])
7+
}
8+
9+
fori:=0;i<len(tickets);i++ {
10+
sort.Strings(adjMap[tickets[i][0]])
11+
}
12+
13+
res:= []string{"JFK"}
14+
15+
vardfsfunc(sourcestring)bool
16+
17+
dfs=func(srcstring)bool {
18+
19+
iflen(res)==len(tickets)+1 {
20+
returntrue
21+
}
22+
23+
if_,ok:=adjMap[src];!ok {
24+
returnfalse
25+
}
26+
27+
destinationList:=adjMap[src]
28+
29+
fori,destination:=rangedestinationList {
30+
31+
adjMap[src]=append(adjMap[src][:i],adjMap[src][i+1:]...)
32+
33+
res=append(res,destination)
34+
ifdfs(destination)==true {
35+
returntrue
36+
}
37+
adjMap[src]=append(adjMap[src][:i+1],adjMap[src][i:]...)
38+
adjMap[src][i]=destination
39+
40+
res=res[:len(res)-1]
41+
}
42+
43+
returnfalse
44+
}
45+
46+
dfs("JFK")
47+
returnres
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp