33 lines
881 B
C++
33 lines
881 B
C++
/**
|
||
* 作者:力扣官方题解
|
||
* 链接:https://leetcode.cn/problems/reconstruct-itinerary/solutions/
|
||
* 来源:力扣(LeetCode)
|
||
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
||
*/
|
||
|
||
class Solution {
|
||
public:
|
||
unordered_map<string, priority_queue<string, vector<string>, std::greater<string>>> vec;
|
||
|
||
vector<string> 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<string> findItinerary(vector<vector<string>>& tickets) {
|
||
for (auto& it : tickets) {
|
||
vec[it[0]].emplace(it[1]);
|
||
}
|
||
dfs("JFK");
|
||
|
||
reverse(stk.begin(), stk.end());
|
||
return stk;
|
||
}
|
||
};
|