71

import java.util.Stack;

class Solution {
    public String simplifyPath(String path) {
        Stack stack = new Stack<>();
        for(String s : path.split("/")){
            if(s.equals("..")){
                if(!stack.empty()){
                    stack.pop();
                }
            }
            else if(!s.isEmpty() && !s.equals(".")){
                stack.push(s);
            }
        }
        String res = "";
        for(String s : stack){
            res = res + "/" + s;
        }
        return res.isEmpty() ? "/" : res;
    }
}

 

转载于:https://www.cnblogs.com/zhaijiayu/p/11551588.html

你可能感兴趣的:(java)