Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
三刷简化了一下代码 使用jdk1.8新特性 switch支持String
public String simplifyPath(String path) {
Stack<String> stack = new Stack<>();
for (String s : path.split("/")) {
switch (s) {
case "" :
case ".":
break;
case "..":
if (!stack.isEmpty()) stack.pop();
break;
default:
stack.push("/"+s);
break;
}
}
return stack.isEmpty() ? "/" : String.join("", stack);
}
需要注意的是
stack extends vector
vector extends abstractlist
所以stack也是iterable的 可以直接用在string.join中