LeetCode之Simplify Path

/*细节模拟题。
参考自:https://github.com/soulmachine/leetcode*/
class Solution {
public:
    string simplifyPath(string path) {
        string res;
        vector<string> path_name;
        string::iterator it = path.begin();
        while(it != path.end()){
            ++it;
            string::iterator itt = find(it, path.end(), '/');
            string dir(it, itt);
            if(!dir.empty() && dir != "."){//dir为有效目录
                if(dir == ".."){//返回上级目录
                    if(!path_name.empty()) path_name.pop_back();
                }
                else{
                    path_name.push_back(dir);
                }
            }
            it = itt;
        }
        if(path_name.empty()) return "/";
        for(int i = 0; i < path_name.size(); ++i)
           res = res +  "/" + path_name[i];
        return res;
    }
};

阅读更多
文章标签: Leetcode
个人分类: Leetcode
上一篇LeetCode之Anagrams
下一篇LeetCode之Length of Last Word
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭