Why my code does not work


  • 0
    P

    '''

    public:

    unordered_map<string, multiset<string>> graph;
    bool helper(vector<string> &res, unordered_map<string, multiset<string>> &graph, int cur, int total){
        if(cur == total)
            return true;
        string pre = res.back();
        multiset<string> hehe = graph[pre];
        for(auto temp : hehe){
            res.push_back(temp);
            graph[pre].erase(graph[pre].find(temp));
            if(helper(res, graph, cur+1, total))
                return true;
            res.pop_back();
            graph[pre].insert(temp);
        }
        return false;
    }
    
    
    vector<string> findItinerary(vector<pair<string, string>> tickets) {
        for(int i=0;i<tickets.size(); i++){
            graph[tickets[i].first].insert(tickets[i].second);
        }
        vector<string> res;
        res.push_back("JFK");
        helper(res, graph, 0, tickets.size());
        return res;
    }
    

    };
    '''
    The above code works. However if I substitute

    multiset<string> hehe = graph[pre];
    for(auto temp : hehe){

    with

    for(auto temp : graph[pre]){

    It fails with runtime error. Could anyone help me explain that? Thanks.


Log in to reply
 

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