zoukankan      html  css  js  c++  java
  • [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");
        }
    }
  • 相关阅读:
    ubuntu 修改hostname
    hadoop "startdfs.sh" WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
    hadoop WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
    ubuntu replace system openjdk
    hadoop install start-dfs.sh 失败
    Centos610无桌面安装Docker-安装
    Centos610无桌面安装Docker-内核升级
    centos610无桌面安装libreoffice缺失字体
    centos610无桌面安装tomcat8
    Centos610无桌面安装VSFTP
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6684991.html
Copyright © 2011-2022 走看看