Skip to content

Commit 084f64e

Browse files
committed
3
1 parent a1c1983 commit 084f64e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

SimplifyPath/SimplifyPath.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
string simplifyPath(string path) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
vector<string> path_stack;
8+
int i = 0;
9+
while (i < path.size()) {
10+
while (i < path.size() && path[i] == '/') i += 1;
11+
if (i == path.size()) break;
12+
string directory;
13+
while (i < path.size() && path[i] != '/') {
14+
directory += path[i];
15+
i += 1;
16+
}
17+
if (directory == "..") {
18+
if (!path_stack.empty())
19+
path_stack.pop_back();
20+
}
21+
else if (directory != ".") {
22+
path_stack.push_back(directory);
23+
}
24+
}
25+
if (path_stack.empty())
26+
return "/";
27+
string simplified_path;
28+
for (int i = 0; i < path_stack.size(); i++)
29+
simplified_path += "/" + path_stack[i];
30+
return simplified_path;
31+
}
32+
};

0 commit comments

Comments
 (0)