71. Simplify Path
可以用String.join 方法 String.join("/", stack);每个元素之间用"/"分开
1 class Solution { 2 public String simplifyPath(String path) { 3 Stack<String> stack = new Stack<String>(); 4 String[] array = path.split("/"); 5 for(String str : array) { 6 if(str.equals("..") && !stack.empty()) { 7 stack.pop(); 8 }else if(str.equals(".") || str.equals("") || str.equals("..")) { //..可能在最前 stack是空的 9 continue; 10 }else { 11 stack.push(str); 12 } 13 } 14 if(stack.empty()) return "/"; 15 String res = ""; 16 // while(!stack.empty()) { 17 // res = "/" + stack.pop() + res; //顺序问题 18 // } 19 res= "/" + String.join("/", stack); 20 return res; 21 22 } 23 }