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

Commit65b8987

Browse files
authored
Merge pull requestneetcode-gh#359 from Samuel-Hinchliffe/main
Create 332-Reconstruct-Itinerary.js
2 parentse5fe18b +3ea4354 commit65b8987

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*@param {string[][]} tickets
3+
*@return {string[]}
4+
*/
5+
varfindItinerary=function(tickets){
6+
constflight_paths=newMap();
7+
constflight_path_order=["JFK"];
8+
9+
tickets=tickets.sort();
10+
11+
for(const[source,dest]oftickets){
12+
letedges=[];
13+
if(flight_paths.has(source)){
14+
edges=flight_paths.get(source);
15+
}
16+
edges.push(dest);
17+
flight_paths.set(source,edges);
18+
}
19+
20+
constdepth_first_search=(city)=>{
21+
if(flight_path_order.length===tickets.length+1)returntrue;
22+
23+
constcities_to_go_to=flight_paths.get(city)||[];
24+
if(!cities_to_go_to.length)returnfalse;
25+
26+
constcities_copied=Array.from(cities_to_go_to);
27+
28+
for(constother_cityofcities_copied){
29+
flight_path_order.push(other_city);
30+
cities_to_go_to.shift();
31+
32+
if(depth_first_search(other_city)){
33+
returnflight_path_order;
34+
}else{
35+
flight_path_order.pop();
36+
cities_to_go_to.push(other_city);
37+
}
38+
}
39+
40+
returnfalse;
41+
};
42+
43+
returndepth_first_search("JFK");
44+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp