leetcode 71 简化路径

简介

简化的linux的路径。

其中我看了java的官方代码很简洁, 使用栈来实现

code

class Solution {
    public String simplifyPath(String path) {
        Stack<String> stack = new Stack();
        String[] string = path.split("/");
        for(String str : string) {
            if(str.isEmpty() || str.equals(".") || (str.equals("..") && stack.isEmpty())) continue;
            if(str.equals("..") && (!stack.isEmpty())) stack.pop(); 
            else {
                stack.push(str);
            }
        }
        return "/" + String.join("/", stack);
    }
}
Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
原文地址:https://www.cnblogs.com/eat-too-much/p/14882458.html