题目:https://leetcode-cn.com/problems/simplify-path/ 参考:https://leetcode-cn.com/problems/simplify-path/solution/cli-yong-stringstreamhe-getlinefen-ge-zi-fu-chuan-/
class Solution {
public:
string simplifyPath(string path) {
/*
*利用getline,每次以/为分隔符
*
*/
stringstream is(path);
vector strs;
string res = "", tmp = "";
while(getline(is, tmp, '/')) {
if(tmp == "" || tmp == ".")
continue;
else if(tmp == ".." && !strs.empty())
strs.pop_back();
else if(tmp != "..")
strs.push_back(tmp);
}
for(string str:strs)
res += "/" + str;
if(res.empty())
return "/";
return res;
}
};