71. Simplify Path

Problem:

思路

Solution (C++):

string simplifyPath(string path) {
    string res, tmp;
    vector<string> my_stack;
    stringstream ss(path);
    while (getline(ss, tmp, '/')) {
        if (tmp == "" || tmp == ".")  continue;
        if (tmp == ".." && !my_stack.empty())  my_stack.pop_back();
        else if (tmp != "..")  my_stack.push_back(tmp);
    }
    for (auto str: my_stack) res += "/" + str;
    return res.empty() ? "/" : res;
}

性能

Runtime: 8 ms  Memory Usage: 10.4 MB

posted @ 2020-02-12 18:41  littledy  阅读(63)  评论(0编辑  收藏  举报