美文网首页
71.简化路径

71.简化路径

作者: _道友请留步_ | 来源:发表于2018-05-15 20:12 被阅读0次
class Solution {
    public String simplifyPath(String path) {
        StringBuilder sb = new StringBuilder();
        Stack<String> stack = new Stack<>();
        String pre = "..", curr = ".";
        //char lastc = ' ';
        String[] paths = path.split("/");
        for(int i = 0; i < paths.length; i++){
            if(pre.equals(paths[i]) ){
                if(!stack.empty()){
                    stack.pop();
                }
            }
            else if(curr.equals(paths[i]) || "".equals(paths[i])){
                continue;
            }
            else {
                stack.push(paths[i]);
            }
        }
        for(int i = 0; i < stack.size(); i++){
            sb.append("/");
            sb.append(stack.get(i));
        }
        if("".equals(sb.toString())){
            return "/";
        }
        return sb.toString();
    }
}

相关文章

网友评论

      本文标题:71.简化路径

      本文链接:https://www.haomeiwen.com/subject/jmiydftx.html