static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
string simplifyPath(string path) { string res, str;
stringstream ss(path);
// 用一个向量存储由'/'分割的路径名
// 遇到..则出栈一个路径,遇到一个路径则入栈
vector<string> v;
while(getline(ss, str, '/')){
if (str == "" || str == ".") continue;
if (str == ".." && !v.empty()) v.pop_back();
else if (str != "..") v.push_back(str);
}
// 最后在每个路径之间添加“/”
for (string str : v)
res += "/" + str;
// 如果路径为空,则直接输出“/”
return res.length() ? res : "/";
}
};
LetCode 71. 简化路径
最新推荐文章于 2024-07-13 22:42:35 发布