71、简化路径

  1. 示例 1:
  2. 示例 2:
  3. 示例 3:
  4. 示例 4:
  5. 示例 5:
  6. 示例 6:
  • 思路——分类整理
  • 以 Unix 风格给出一个文件的绝对路径,你需要简化它。或者换句话说,将其转换为规范路径。

    在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。更多信息请参阅:Linux / Unix中的绝对路径 vs 相对路径

    请注意,返回的规范路径必须始终以斜杠 / 开头,并且两个目录名之间必须只有一个斜杠 /。最后一个目录名(如果存在)不能以 / 结尾。此外,规范路径必须是表示绝对路径的最短字符串。

    示例 1:

    输入:"/home/"
    输出:"/home"
    

    解释:注意,最后一个目录名后面没有斜杠。

    示例 2:

    输入:"/../"
    输出:"/"
    

    解释:从根目录向上一级是不可行的,因为根是你可以到达的最高级。

    示例 3:

    输入:"/home//foo/"
    输出:"/home/foo"
    

    解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。

    示例 4:

    输入:"/a/./b//c/"
    输出:"/c"
    

    示例 5:

    输入:"/a//b/../c//.//"
    输出:"/c"
    

    示例 6:

    输入:"/a//b////c/d//././/.."
    输出:"/a/b/c"
    

    思路——分类整理

    • /a/../ -> /
    • ./ -> /
    • // -> /
    • /xxx/ -> /xxx
    • /a/.. -> /
    • 遍历一遍,完成简化、用栈存储结果
      • 从右至左遍历,简化、跳过
      • 从左至右遍历,简化、回退
    public class Leedcode71 {
        public static void main(String[] args) {
            Leedcode71 leedcode71 = new Leedcode71();
            String res =  leedcode71.simplifyPath("/home");
            System.out.println(res);
            res =  leedcode71.simplifyPath("/../");
            System.out.println(res);
            res =  leedcode71.simplifyPath("/home//foo/");
            System.out.println(res);
            res =  leedcode71.simplifyPath("/a/./b//c/");
            System.out.println(res);
            res =  leedcode71.simplifyPath("/a//b/../c//.//");
            System.out.println(res);
            res =  leedcode71.simplifyPath("/a/b////c/d//././/..");
            System.out.println(res);
            System.out.println("--------------------------------");
            res =  leedcode71.simplifyPath1("/home");
            System.out.println(res);
            res =  leedcode71.simplifyPath1("/../");
            System.out.println(res);
            res =  leedcode71.simplifyPath1("/home//foo/");
            System.out.println(res);
            res =  leedcode71.simplifyPath1("/a/./b//c/");
            System.out.println(res);
            res =  leedcode71.simplifyPath1("/a//b/../c//.//");
            System.out.println(res);
            res =  leedcode71.simplifyPath1("/a/b////c/d//././/..");
            System.out.println(res);
        }
    
        public String simplifyPath(String path) {
            StringBuilder sb = new StringBuilder();
            Stack<String> sk = new Stack<String>();
            String[] paths = path.split("/");
            int jump = 0;
            for (int i = paths.length - 1;i >= 0;i--){
                if (paths[i].equals("") || paths[i].equals(".")){
                    continue;
                }else if(paths[i].equals("..")){
                    jump++;
                }else {
                    if (jump == 0){
                        sk.add(paths[i]);
                    } else {
                        jump--;
                    }
                }
            }
            if (sk.isEmpty()){
                sb.append("/");
            }
            while(!sk.isEmpty()){
                sb.append("/" + sk.pop());
            }
            return sb.toString();
        }
    
        public String simplifyPath1(String path) {
            String[] s = path.split("/");
            Stack<String> stack = new Stack<>();
            
            for (int i = 0; i < s.length; i++) {
                if (!stack.isEmpty() && s[i].equals(".."))
                    stack.pop();
                else if (!s[i].equals("") && !s[i].equals(".") && !s[i].equals(".."))
                    stack.push(s[i]);
            }
            if (stack.isEmpty())
                return "/";
    
            StringBuffer res = new StringBuffer();
            for (int i = 0; i < stack.size(); i++) {
                res.append("/" + stack.get(i));
            }
            return res.toString();
        }
    
    // 作者:StackOverflow-
    // 链接:https://leetcode-cn.com/problems/simplify-path/solution/java-yi-dong-yi-jie-xiao-lu-gao-by-spirit-9-18/
    // 来源:力扣(LeetCode)
    // 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    }
    

    转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

    💰

    Title:71、简化路径

    Count:835

    Author:攀登

    Created At:2020-07-26, 00:19:44

    Updated At:2024-06-15, 15:52:32

    Url:http://jiafeimao-gjf.github.io/2020/07/26/71%E3%80%81%E7%AE%80%E5%8C%96%E8%B7%AF%E5%BE%84/

    Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

    ×

    Help us with donation