leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0797.cpp (915B)


0 class Solution { 1 public: 2 vector<vector<int>> allPathsSourceTarget(vector<vector<int>> &graph) { 3 int n = graph.size(); 4 5 vector<vector<int>> res; 6 unordered_set<int> visited; 7 vector<int> path; 8 stack<int> st; 9 10 st.push(0); 11 while (!st.empty()) { 12 int root = st.top(); 13 st.pop(); 14 15 if (root == n - 1) { 16 path.push_back(root); 17 res.push_back(path); 18 path.pop_back(); 19 continue; 20 } 21 22 if (visited.count(root)) { 23 visited.erase(root); 24 path.pop_back(); 25 continue; 26 } 27 28 path.push_back(root); 29 visited.insert(root); 30 st.push(root); 31 32 for (int n : graph[root]) 33 if (!visited.count(n)) st.push(n); 34 } 35 36 return res; 37 } 38 };