• [LeetCode]Simplify Path


    题目:Simplify Path

    简化路径。

    1.回到父目录时要合并,去掉父目录。

    2.去掉多余的'/'

    3.去掉最后的一个'/'

    4.去掉多余的'.'

    5."/."和"/.."这两种情况也要特殊考虑。

    package com.example.medium;
    
    public class SimplifyPath {
        //判断当前遇到的'/'是否表示返回父目录,即"../"(开头)或"/../"
        private boolean checkBackUp(String path,int i){
            if(i < 2)return false;
            if(path.charAt(i - 1) == '.' && path.charAt(i - 2) == '.'){
                if(i == 2)return true;
                if(path.charAt(i - 3) == '/')return true;
            }
            return false;
        }
        //判断当前遇到的'/'是否表示当前目录,即"./"(开头)或"/./"冗余的"./"
        private boolean checkCurrentDir(String path,int i){
            if(i < 1)return false;
            if(path.charAt(i - 1) == '.'){
                if(i == 1)return true;
                if(path.charAt(i - 2) == '/')return true;//防止文件名中含有"."的情况
            }
            return false;
        }
        public String simplifyPath(String path) {
            char pathArray[] = new char[path.length()];
            int index = 0;
            char ch;
            for(int i = 0;i < path.length();i++){
                ch = path.charAt(i);
                if(ch == '/'){
                    if(i > 0 && path.charAt(i - 1) == '/')continue;//冗余'/'
                    if(checkCurrentDir(path, i)){//是冗余的当前目录"./"
                        index = index - 1;//坐标向后移动,覆盖掉已复制的"."
                        continue;
                    }
                    if(checkBackUp(path, i)){//是返回父目录
                        index -= 4;//坐标回退4
                        index = index < 0 ? 0 : index;//小于零的情况
                        while(index > 0 && pathArray[index] != '/')index--;//去掉当前文件名
                        if(pathArray[index] == '/')index++;//退回到父目录
                        continue;
                    }
                }
                pathArray[index++] = ch;
            }
            if(pathArray[index - 1] == '.'){//"/."或"/.."的特殊情况
                int lastNotNum = index - 1;
                while(lastNotNum > 0 && pathArray[lastNotNum] == '.')lastNotNum--;
                if(pathArray[lastNotNum] == '/' && index - lastNotNum < 4){
                    if(index - lastNotNum == 3){//"/.."的特殊情况
                        if(lastNotNum > 0)lastNotNum--;
                        while(lastNotNum > 0 && pathArray[lastNotNum] != '/')lastNotNum--;
                    }
                    index = lastNotNum + 1;
                }
            }
            //去掉最后的"/",如果存在
            if(index > 0 && pathArray[index - 1] == '/')pathArray[--index] = 0;
            if(pathArray[0] != '/')return new StringBuilder("/").append(pathArray,0,index).toString();
            return new StringBuilder().append(pathArray,0,index).toString();
        }
    
        public static void main(String args[]){
            long startTime = System.currentTimeMillis();
            System.out.println(new SimplifyPath().simplifyPath(new String("/.")));
            long endTime = System.currentTimeMillis();
            System.out.println("程序运行时间:"+(endTime-startTime) + "ms");
        }
    }
  • 相关阅读:
    【特种兵系列】String中的==和equals()
    windows7 x64下maven安装和配置
    微信开发之本地接口调试(非80端口,无需上传服务器服务器访问)
    Java微信公众平台接口封装源码分享
    jdk安装
    用户权限管理
    Linux常用命令(一)
    Xshell显示中文乱码问题
    伪装虽易测试不易之微信浏览器
    每次新建项目出现appcompat_v7 解决方法
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6684991.html
Copyright © 2020-2023  润新知