/** * 作者:力扣官方题解 * 链接:https://leetcode.cn/problems/reconstruct-itinerary/solutions/ * 来源:力扣(LeetCode) * 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 */ class Solution { public: unordered_map, std::greater>> vec; vector stk; void dfs(const string& curr) { while (vec.count(curr) && vec[curr].size() > 0) { string tmp = vec[curr].top(); vec[curr].pop(); dfs(move(tmp)); } stk.emplace_back(curr); } vector findItinerary(vector>& tickets) { for (auto& it : tickets) { vec[it[0]].emplace(it[1]); } dfs("JFK"); reverse(stk.begin(), stk.end()); return stk; } };