[leetcode]71. 简化路径

71. 简化路径

唯一的坑点,"/..."这个示例,"..."是目录名

class Solution {
public:
    string simplifyPath(string path) {
        deque<string> DQ;
        istringstream iss(path);
        string word;
        while(getline(iss, word, '/')) {
            if(!word.empty() && word != "." && word != "..")
                DQ.push_back(word);
            else if(!word.empty() && word == ".." && !DQ.empty())
                DQ.pop_back();
        }
        if(DQ.empty())
            return "/";
        else {
            string ret;
            while(!DQ.empty()) {
                ret += "/";
                ret += DQ.front();
                DQ.pop_front();
            }
            return ret;
        }
    }
};
原文地址:https://www.cnblogs.com/pusidun/p/13644021.html