-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimplify Path
More file actions
55 lines (47 loc) · 1.54 KB
/
Simplify Path
File metadata and controls
55 lines (47 loc) · 1.54 KB
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
53
54
55
/********************************************
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.
Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
***************************************/
class Solution {
public:
string simplifyPath(string path) {
int start,end;
int l = path.size();
stack<string> mystack;
string result;
start = 0;
while(start < l)
{
if(path[start] == '/')
{
end = start+1;
while(path[end]!='/'&&end<l) end++;
string s = path.substr(start+1,end-start-1);
printf("%s\n",s.c_str());
if(s == ".." && mystack.empty()){start = end;continue;}
if(s == ".." && !mystack.empty()) mystack.pop();
else if (s!="." && !s.empty())
mystack.push(s);
start = end;
}
else
start++;
}
if(mystack.empty()) return "/";
while(!mystack.empty())
{
result.insert(0,mystack.top());
result.insert(0,"/");
mystack.pop();
}
return result;
}
};