71. Simplify Path

class Solution {
    public String simplifyPath(String path) {
        Stack<String> stack=new Stack<String>();
        String[] strs=path.split("\/");
        for(int i=0;i<strs.length;i++)
        {
            if(strs[i].length()==0||strs[i].equals("."))
                continue;
            else if(strs[i].equals(".."))
            {
                if(!stack.isEmpty())
                    stack.pop();
            }
            else
                stack.push(strs[i]);
        }
        StringBuilder sb=new StringBuilder();
        while(!stack.isEmpty())
            sb.insert(0, "/"+stack.pop());
        return sb.length()==0?"/":sb.toString();
    }
}
原文地址:https://www.cnblogs.com/asuran/p/7594774.html