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 26 27 28 29 30 | public class Solution { public String simplifyPath(String path) { Stack<String> stack = new Stack<String>(); String temp = ""; path = path+"/"; for (int i = 0; i < path.length(); i++){ char current = path.charAt(i); if (current == ' ') continue; if (current == '/'){ if (temp.equals("..")){ if (!stack.isEmpty()){ stack.pop(); } }else if(!temp.equals(".") && !temp.equals("")){ stack.push(temp); } temp = ""; }else{ temp += current; } } if (stack.isEmpty()) return "/"; String result = ""; while (!stack.isEmpty()){ result = "/"+stack.pop()+result; } return result; } } |
Tuesday, December 1, 2015
[leetcode] Simplify Path
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment