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");
        }
    }
  • 相关阅读:
    IDEA最常用快捷键汇总+快速写出Main函数
    设计模式之代理模式
    Java多线程中join、yield、sleep方法详解
    git基础命令详解
    用友网络科技Java高级开发面试题(2019)
    Java内部类超详细总结(含代码示例)
    构造器中绝对绝对不能调用可被覆盖的方法
    写给小白看的Git的安装配置与使用
    Vue学习笔记5--前端工程化
    Vue学习笔记4--vue-router路由
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6684991.html
Copyright © 2011-2022 走看看