题目
Leetcode 71. 简化路径
代码(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;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25