[leetcode]71. Simplify Path

Solution 1:

genius,我的想法是老老实实一个一个char压栈,结果应该先分割!!!

class Solution {
    public String simplifyPath(String path) {
    Stack<String> stack = new Stack();
    for(String cur: path.split("/")){
        if(cur.equals("..")) {
            if(!stack.empty()) stack.pop();
        }
        else if(cur.length()>0 && !cur.equals(".")) stack.push(cur);
    }
    return "/"+String.join("/",stack);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_36869329/article/details/85238486