12345678910111213141516171819202122232425262728
class Solution { public: vector<string> solution; vector<string> findItinerary(vector<pair<string, string>> tickets) { solution.reserve(tickets.size()); solution.push_back("JFK"); while (!tickets.empty()) { int tgt = -1; for (int i = 0; i < tickets.size(); ++i) { if (!tickets[i].first.compare(solution[solution.size()-1])) { if (tgt == -1) { tgt = i; } else if (tickets[tgt].second.compare(tickets[i].second) > 0) { tgt = i; } } } solution.push_back(tickets[tgt].second); tickets.erase(tickets.begin()+tgt); } return solution; } };
solution
solution.reserve()
tickets