Leetcode 71. 简化路径

用栈模拟

class Solution {
public:
    string simplifyPath(string path) {
        vector<string> sta;
        string s;
        for (auto &x : path)
            if (x == '/') x = ' ';
        stringstream ss(path);
        while (ss >> s) {
            if (s == "..") {
                if (!sta.empty()) sta.pop_back();
            }
            else if (s != ".") sta.push_back(s);
        }
        s = "";
        if (sta.empty()) return "/";
        for (auto &x : sta)
            s += "/" + x;
        return s;
    }
};

猜你喜欢

转载自blog.csdn.net/bendaai/article/details/80298913
今日推荐