解题思路
利用栈的思想,去除掉字符串中的".","",如果遇到"…"就将栈顶弹出即可
代码
class Solution {
public String simplifyPath(String path) {
String[] mem = path.split("/");
LinkedList<String> stack = new LinkedList<>();
StringBuilder res = new StringBuilder();
for (String str : mem){
//弹出当前目录
if (str.equals("..")){
if (!stack.isEmpty()){
stack.removeLast();
}else {
continue;
}
}
else if (str.length() > 0 && !".".equals(str)) {
stack.add(str);
}
}
if (stack.isEmpty()){
res.append("/");
}else {
while (!stack.isEmpty()){
res.append("/");
res.append(stack.getFirst());
stack.removeFirst();
}
}
return res.toString();
}
}