-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplifyPath.java
More file actions
62 lines (55 loc) · 1.84 KB
/
SimplifyPath.java
File metadata and controls
62 lines (55 loc) · 1.84 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
56
57
58
59
60
61
62
public class SimplifyPath {
public String simplifyPath(String path) {
String p = path + '/';
Stack<String> ss = new Stack<String>();
StringBuffer tmp = new StringBuffer();
for(int i = 0; i < p.length(); i++){
char ch = p.charAt(i);
if(ch == '/'){
if(i > 2 && p.charAt(i -1) == '.' && p.charAt(i -2) == '.' &&
( p.charAt(i - 3) == '/')){
if(!ss.isEmpty()) ss.pop();
}else if((i > 0 && p.charAt(i -1) == '/') ||
(i > 1 && p.charAt(i -2) != '.' && p.charAt(i -1) == '.')){
}else {
if(i > 0)ss.push(tmp.toString());
}
tmp = new StringBuffer("/");
}else {
tmp.append(ch);
}
}
if (ss.size() == 0) return "/";
StringBuffer buf = new StringBuffer();
while (!ss.isEmpty()) {
buf.insert(0, ss.pop());
}
return buf.toString();
}
/*
public String simplifyPath(String path) {
if (path == null || path.length() == 0) return "/";
Stack<String> stack = new Stack<String>();
String[] splits = path.trim().split("/");
for (String s : splits) {
if (s == null || s.length() == 0 || s.equals(".")) {
// Do nothing;
} else if (s.equals("..")) {
// Pop if stack is not empty;
if (stack.size() > 0) stack.pop();
} else {
// Push all others to stack.
stack.push(s);
}
}
// Remember, stack can be empty.
if (stack.isEmpty()) return "/";
StringBuffer buf = new StringBuffer();
while (!stack.isEmpty()) {
buf.insert(0, stack.pop());
buf.insert(0, "/");
}
return buf.toString();
}
*/
}