文章目录
- 题目
- 代码(9.28 首刷调试看解析)
题目
文章来源:https://uudwc.com/A/nPL30
Leetcode 71. 简化路径文章来源地址https://uudwc.com/A/nPL30
代码(9.28 首刷调试看解析)
class Solution {
public:
string simplifyPath(string path) {
vector<string> parts;
int start = 0;
for(int i = 1; i <= path.size(); ++i) {
if(path[i] == '/' || i == path.size()) {
string part = path.substr(start+1, i-start-1);
if(part == "" || part == ".") {
} else if(part == "..") {
if(!parts.empty()) parts.pop_back();
} else {
parts.push_back(part);
}
start = i;
}
}
string res;
for(string& s : parts) {
res += "/" + s;
}
return res == "" ? "/" : res;
}
};