What's wrong with my code ? C++


  • 0
    W
    class Solution {
    public:
        static bool cmp(pair<string, string> a, pair<string, string> b) {
            return a.first < b.first || a.second < b.second;
        }
        
        vector<string> findItinerary(vector<pair<string, string>> tickets) {
            sort(tickets.begin(), tickets.end(), cmp);
            vector<string> result;
            dfs(tickets, "JFK", result, (int)tickets.size()+1);
            return result;
        }
        
        void dfs(vector<pair<string, string>> tickets, string cur, vector<string>& result, int len) {
            if (tickets.empty()) {
                result.push_back(cur);
                return;
            }
            
            for (int i=0; i<tickets.size(); i++) {
                if (tickets[i].first == cur) {
                    pair<string, string> tmp = tickets[i];
                    result.push_back(cur);
                    tickets.erase(tickets.begin()+i);
                    dfs(tickets, tmp.second, result, len);
                    if (result.size() == len) break;
                    tickets.insert(tickets.begin()+i, tmp);
                    result.pop_back();
                }
            }
            
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.