
Problem Description
还是没有一次AC。挂在了出栈那个地方。因为输入可能是些奇奇怪怪的路径,所以,出栈之前先对栈做一下判断。
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
|
public String (String path) { if (path == null || path.length() == 0) return path; Stack<String> stack = new Stack<String>(); String[] strs = path.split("/"); for (String str : strs) { if (str.equals("") || str.equals(".")) continue; else if (str.equals("..")) { if (!stack.isEmpty()) stack.pop(); } else stack.push(str); } StringBuilder sb = new StringBuilder(); while (!stack.isEmpty()) { sb.insert(0,stack.pop()); sb.insert(0, "/"); } return sb.length() == 0 ? "/" : sb.toString(); }
|
近期评论