本文主要是介绍Java | Leetcode Java题解之第71题简化路径,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution {public String simplifyPath(String path) {String[] names = path.split("/");Deque<String> stack = new ArrayDeque<String>();for (String name : names) {if ("..".equals(name)) {if (!stack.isEmpty()) {stack.pollLast();}} else if (name.length() > 0 && !".".equals(name)) {stack.offerLast(name);}}StringBuffer ans = new StringBuffer();if (stack.isEmpty()) {ans.append('/');} else {while (!stack.isEmpty()) {ans.append('/');ans.append(stack.pollFirst());}}return ans.toString();}
}
这篇关于Java | Leetcode Java题解之第71题简化路径的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!