leetcode-----71. 简化路径

代码

class Solution {
public:
    string simplifyPath(string path) {
        string ans, name;
        if (path.back() != '/') path += '/';
        for (auto c: path) {
            if (c != '/') name += c;
            else {
                if (name == "..") {
                    while (ans.size() && ans.back() != '/') ans.pop_back();
                    if (ans.size()) ans.pop_back();
                } else if (name != "." && name != "") {
                    ans += '/' + name;
                } 
                name.clear();
            }
        }
        if (ans.empty()) ans = "/";
        return ans;
    }
};
posted @ 2020-07-11 10:50  景云ⁿ  阅读(55)  评论(0编辑  收藏  举报