Medium
要先懂幾個基本的Unix-style符號的意思:
.
current directory
..
parent directory
class Solution {
public String simplifyPath(String path) {
Stack<String> stack = new Stack<>();
Set<String> set = new HashSet<>(Arrays.asList("..",".",""));
for (String s : path.split("/")){
if (s.equals("..") && !stack.isEmpty()){
stack.pop();
}
if (!set.contains(s)){
stack.push(s);
}
}
String res = "";
for (String s : stack){
res += "/" + s;
}
return res.length() == 0 ? "/" : res;
}
}