-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (36 loc) · 1.08 KB
/
Solution.java
File metadata and controls
39 lines (36 loc) · 1.08 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
package $071;
import java.util.LinkedList;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 10:25 AM 2018/08/09.
*/
public class Solution {
public String simplifyPath(String path) {
if (path == null || path.length() == 0){
return path;
}
String[] strs = path.split("/");
LinkedList<String> linkedList = new LinkedList<>();
for (String str : strs){
if ("..".equals(str)){
if (!linkedList.isEmpty()){
linkedList.removeLast();
}
}else if ((!".".equals(str)) && (!"".equals(str))){
linkedList.add(str);
}
}
if (linkedList.size() == 0){
return "/";
}
StringBuilder stringBuilder = new StringBuilder();
for (String str : linkedList){
stringBuilder.append("/" + str);
}
return stringBuilder.toString();
}
public static void main(String[] args) {
Solution solution = new Solution();
solution.simplifyPath("/home/");
}
}