题目链接:hhttps://leetcode-cn.com/problems/simplify-path/
以/
为分隔符分别读取,分别判断即可,使用sstream头文件,istringstream+getline方法
string simplifyPath(string path){
string res, s;
for (auto c : path) {
if (res.empty()) res += c;
else if (c != '/') s += c;
else {
if (s == "..") {
if (res.size() > 1) {
res.pop_back();
while (res.back() != '/') {
res.pop_back();
}
}
} else if (s != "." && s != "") {
res += s + '/';
}
s = "";
}
}
if (res.size() > 1) res.pop_back();
return res;
}