-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParsePath.java
39 lines (29 loc) · 959 Bytes
/
ParsePath.java
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
/*https://leetcode.com/problems/simplify-path/*/
class Solution {
public String simplifyPath(String path) {
Deque<String> stack = new ArrayDeque<String>();
String[] tokens = path.split("/");
for(int i = 0; i < tokens.length; ++i)
{
String str = tokens[i];
if(str.length() == 0 || str.equals("."))
continue;
if(str.equals(".."))
if(!stack.isEmpty())
stack.pop();
else
stack.push(str);
}
if(stack.isEmpty())
return "/";
StringBuilder re = new StringBuilder();
while(!stack.isEmpty())
{
re.insert(0,"/");
re.insert(0,stack.pop());
}
re.insert(0,"/");
re.deleteCharAt(re.length()-1);
return re.toString();
}
}