-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplify_Path.java
More file actions
32 lines (20 loc) · 884 Bytes
/
Simplify_Path.java
File metadata and controls
32 lines (20 loc) · 884 Bytes
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
public class Solution {
public String simplifyPath(String path) {
if(path == null) return null;
else if(path.length() == 0) return "";
LinkedList<String> res = new LinkedList<String>();
char[] charPath = path.toCharArray();
for(int i = -1;i < charPath.length;){
String tmp = "/";
while(((++i) < charPath.length) && (charPath[i] != '/')) tmp += charPath[i];
if(tmp.equals("/.") || tmp.equals("/")) continue;
if(tmp.equals("/..")){
if(!res.isEmpty()) res.removeLast();
}
else res.add(tmp);
}
String result = "";
for(String str : res) result += str;
return res.isEmpty() ? "/" : result;
}
}