# Leetcode [No. 1436] Destination City (EASY) ## 題目 https://leetcode.com/problems/destination-city/submissions/?envType=daily-question&envId=2023-12-15 ## 思路 這個解法基本就是靠一個hashMap當成主要的資料結構。 + 先用一個map紀錄每一個pair。 + 接著把第一個終點放進hashMap去做尋找下一個站 + 因為題目是沒有loop! + 否則可能會造成無限迴圈。 ```c++= class Solution { public: string destCity(vector<vector<string>>& paths) { unordered_map<string, string> hashMap; for (int i = 0; i < paths.size(); i++) { hashMap[paths[i][0]] = paths[i][1]; } string curCity = hashMap[paths[0][0]]; /* for (const auto& n : hashMap) { std::cout << "first: " << n.first << ", second: " << n.second << "\n"; } */ while(hashMap.count(curCity)>0) { curCity = hashMap[curCity]; } return curCity; } }; ``` ### 解法分析 + time complexity: O(n) ### 執行結果 ![image](https://hackmd.io/_uploads/HyBz45FUp.jpg)