-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-71-Simplify_Path-V2.cpp
52 lines (43 loc) · 1.1 KB
/
leetcode-71-Simplify_Path-V2.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdlib>
#include <string>
#include <stack>
class Solution {
public:
std::string simplifyPath(std::string path) {
std::string res = "";
for(size_t i = 0; i<path.size(); ++i)
{
std::string temp_path = "";
while(i<path.size() && path[i]!='/')
{
temp_path += path[i++];
}
if (temp_path == "..") {
while(!res.empty() && res[res.size()-1]!='/')
{
res.pop_back();
}
if(!res.empty())
{
res.pop_back();
}
} else if (temp_path.empty() || temp_path == ".") {
continue;
} else {
if(!res.empty())
{
res+='/';
}
res+=temp_path;
}
}
return '/'+res;
}
};
int main(){
std::string path = "/home/";
auto res = Solution().simplifyPath(path);
std::cout << res << std::endl;
return EXIT_SUCCESS;
}