Could anyone tell me why I'm getting Runtime in the case 1 [], thank you very much!


  • 0
    B
    int visitado[100000];
    vector<int>res;
    list<int>lista[100000],lista2[100000];
    class Solution {
    public:
        vector<int> findOrder(int numCourses, vector<pair<int, int> > & prerequisites) {
    
                res.clear();
                int v[numCourses];
                for(int i = 0;i<numCourses;i++)
                {
                    lista[i].clear();
                    lista2[i].clear();
                    v[i]=0;
                    visitado[i]=-1;
                }
    
                for(int i=0;i<prerequisites.size();i++)
                {
                    lista[prerequisites[i].second].push_back(prerequisites[i].first);
                    lista2[prerequisites[i].first].push_back(prerequisites[i].second);
                    v[prerequisites[i].first]++;
                }
                res.clear();
                for(int i=0;i<numCourses;i++)
                {
                    if(v[i] == 0)
                    {
                        dfs(i,true);
                    }
                }
        }
        void dfs(int pos,bool t)
        {
            if(t)
            {
                res.push_back(pos);
                visitado[pos] = 1;
                for(std::list<int>::iterator it=lista[pos].begin(); it != lista[pos].end(); ++it)
                {
                   dfs(*it,false);
                }
            }
            else
            {
                for(std::list<int>::iterator it=lista2[pos].begin(); it != lista2[pos].end(); ++it)
                {
                    if(visitado[*it] == -1)return;
                }
                res.push_back(pos);
                visitado[pos] = 1;
                for(std::list<int>::iterator it=lista[pos].begin(); it != lista[pos].end(); ++it)
                {
                   dfs(*it,false);
                }
            }
        }
    };

  • 0
    1

    I didn't see your code clearly, but I think here it is absolutely wrong: "int v[numCourses];"
    numCourses is not a const expression, you can not use it when you define an array.


Log in to reply
 

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