71

import java.util.Stack;

class Solution {
    public String simplifyPath(String path) {
        Stack<String> 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;
    }
}

猜你喜欢

转载自www.cnblogs.com/zhaijiayu/p/11551588.html
71
71A