Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
Code shrimp is a sand carving and funny boy who likes listening to music, playing games and writing as well as most of his friends. The days are still very long, let’s refuel our efforts together 🌈
✨ topic
Power button link
The question has beenForce button brush column included!
1436. Tour terminus
Paths [I] = [cityAi, cityBi] indicates that the paths will go directly from cityAi to cityBi. Find the final destination of the tour, a city that does not have any route to any other city.
The problem data guarantees that the route diagram will form a circuit with no loops, so that there is exactly one travel destination.
Example 1:
Enter: Paths = [[“London”,”New York”],[“New York”,”Lima”],[“Lima”,”Sao Paulo”] It starts at London and ends at Sao Paulo. The itinerary for this trip is “London” -> “New York” -> “Lima” -> “Sao Paulo”.
Example 2:
Input: paths = [[” B “, “C”], [” D “, “B”], [” C “, “A”]] output: “A” explanation: all possible routes are: “D” -> “B” -> “C” -> “A”. “B” -> “C” -> “A”. “C” -> “A”. “A”. Obviously, the end of the trip is “A”.
Example 3:
Paths = [[“A”,”Z”]]
Tip:
1 <= paths.length <= 100 paths[i].length == 2 1 <= cityAi.length, cityBi.length <= 10 cityAi ! = cityBi All characters consist of uppercase and lowercase letters and Spaces.
🔥 : hash table
They already say:A city without any lines to other cities.
That is to sayThere is only one key station
:Starting from any of these stations can lead to the final site
So we use hash table simulation:K:V -- > Current site: next site
, so we can keep all the mappings, so we can start at any site
🔥 If the current site does not have another site, the site is an important site.
✨ code implementation
class Solution {
public String destCity(List<List<String>> paths) {
Map<String, String> map = new HashMap<>();
int len = paths.size();
for (int i = 0; i < len; i++) {
map.put(paths.get(i).get(0),paths.get(i).get(1));
}
String value = paths.get(0).get(1);
while (map.containsKey(value)) {
value = map.get(value);
}
returnvalue; }}Copy the code
💖 finally
I am aCode pipi shrimp, a prawns lover who loves to share knowledge, will update useful blog posts in the future, looking forward to your attention!!
Creation is not easy, if this blog is helpful to you, I hope you can == one key three even oh! Thank you for your support. See you next time