Leetcode 71. Simplify Path (Medium) (cpp)

来源:互联网 发布:实际投资算法 编辑:程序博客网 时间:2024/06/17 03:23

Leetcode 71. Simplify Path (Medium) (cpp)

Tag: Stack, String

Difficulty: Medium


/*71. Simplify Path (Medium)Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:Did you consider the case where path = "/../"?In this case, you should return "/".Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".In this case, you should ignore redundant slashes and return "/home/foo".*/class Solution {public:string simplifyPath(string path) {string res = "", token;stringstream ss(path);vector<string> pos;while(getline(ss, token, '/')){if(token == "." || token == "") continue;else if(token==".."){if(!pos.empty())  pos.pop_back();}else pos.push_back(token);}if(pos.empty()) return "/";for(string i : pos)res = res + '/' + i;return res;}};


0 0
原创粉丝点击