题目:
https://leetcode-cn.com/problems/simplify-path/
import java.util.ArrayDeque; import java.util.Deque; public class _71_SimplifyPath { public String simplifyPath(String path) { Deque<String> stack = new ArrayDeque<>(); String[] paths = path.split("/"); for (int i = 0; i < paths.length; i++) { String name = paths[i]; if (name.equals("")) { continue; } else if (name.equals(".")) { continue; } else if (name.equals("..")) { if (!stack.isEmpty()) { stack.pop(); } } else { stack.push(name); } } String ans = ""; int size = stack.size(); for (int i = 0; i < size; i++) { ans += "/"; ans += stack.pollLast(); } if (ans.equals("")) { ans += "/"; } return ans; } }