leetcode

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

0071.cpp (736B)


      1 class Solution {
      2   public:
      3     string simplifyPath(string path) {
      4         deque<string> dq;
      5         int start = 0;
      6 
      7         path.push_back('/');
      8         for (int i = 0; i < path.size(); i++) {
      9             if (path[i] != '/')
     10                 continue;
     11             else {
     12                 string s = path.substr(start, i - start);
     13                 if (s == "..") {
     14                     if (!dq.empty()) dq.pop_back();
     15                 } else if (!s.empty() && s != ".")
     16                     dq.push_back(s);
     17                 start = i + 1;
     18             }
     19         }
     20 
     21         string res = "";
     22         while (!dq.empty()) {
     23             res += "/" + dq.front();
     24             dq.pop_front();
     25         }
     26         return res.empty() ? "/" : res;
     27     }
     28 };