-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplifyPath.java
42 lines (37 loc) · 1.08 KB
/
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
37
38
39
40
41
42
package simplifyPath;
import java.awt.List;
import java.util.LinkedList;
public class SimplifyPath {
public String simplifyPaht(String path){
if(path.length() == 0){
return path;
}
String[] splits = path.split("/");
LinkedList<String> stack = new LinkedList<String>();
for (String s : splits) {
if(s.length()==0 || s.equals(".")){
continue;
}else if(s.equals("..")){
if(!stack.isEmpty()){
stack.pop();
}
}else{
stack.push(s);
}
}
if(stack.isEmpty()){
stack.push("");
}
String ret = "";
while(!stack.isEmpty()){
ret += "/" + stack.removeLast();
}
return ret;
}
public static void main(String[] args){
SimplifyPath t = new SimplifyPath();
String path = "/abc/...";
String res = t.simplifyPaht(path);
System.out.println(res);
}
}