public class Solution {
public String simplifyPath(String path) {
if (path.length()==0) return path;
String[] split = path.split("/");
LinkedList<String> stack = new LinkedList<String> ();
for (String s:split) {
if (s.length()==0 || s.equals(".")) {
continue;
} else if (s.equals("..")) {
if (!stack.isEmpty()) stack.pop();
} else stack.push(s);
}
if (stack.isEmpty()) stack.push("");
String res = "";
while (!stack.isEmpty()) res += "/"+stack.removeLast();
return res;
}
}
没有评论:
发表评论