71. Simplify Path

Posted The Tech Road

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了71. Simplify Path相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/simplify-path/description/

class Solution {
public:
    string simplifyPath(string path) {
        int idx = 0;
        vector<string> s;
        while (true) {
            string token = getToken(path, idx);
            if (token == "")    break;
            if (token == ".")   continue;
            if (token == "..") {
                if (s.size() > 0)
                    s.pop_back();
            }
            else
                s.push_back(token);
        }
        
        string res;
        for (auto& t : s) {
            res += "/" + t;
        }
        return res == "" ? "/" : res;
    }
    string getToken(const string& path, int& idx) {
        while (idx < path.length() && path[idx] == /) idx++;
        if (idx == path.length())   return "";
        int end = idx;
        while (end < path.length() && path[end] != /) end++;
        string res = path.substr(idx, end-idx);
        idx = end;
        return res;
    }
};

 

以上是关于71. Simplify Path的主要内容,如果未能解决你的问题,请参考以下文章

71. Simplify Path

leetcode 71 Simplify Path

71. Simplify Path

71. Simplify Path

71. Simplify Path

71. Simplify Path