-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path121_simplifyPath.java
36 lines (36 loc) · 995 Bytes
/
121_simplifyPath.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
public class Solution {
/**
* @param path the original path
* @return the simplified path
*/
public String simplifyPath(String path) {
// Write your code here
if(path==null||path.length()==0)
return "";
StringBuilder result = new StringBuilder();
String[] paths = path.split("/");
LinkedList<String> stack = new LinkedList<String>();
for(int i=0; i<paths.length; i++){
String tmp = paths[i];
if(tmp.equals(""))
continue;
if(tmp.equals("."))
continue;
if(tmp.equals("..")){
if(i>=2){
stack.remove(stack.get(stack.size()-1));
}
continue;
}
stack.add(tmp);
}
int size = stack.size();
result.append("/");
for(int j=0; j<size; j++){
result.append(stack.get(j));
if(j!=size-1)
result.append("/");
}
return result.toString();
}
}