[leedcode 71] Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

public class Solution {
    public String simplifyPath(String path) {
        /*这道题目是Linux内核中比较常见的一个操作,就是对一个输入的文件路径进行简化。
        思路比较明确,就是维护一个栈,对于每一个块(以‘/’作为分界)进行分析,如果遇到‘../’则表示要上一层,那么就是进行出栈操作,
        如果遇到‘./’则是停留当前,直接跳过,其他文件路径则直接进栈即可。
        最后根据栈中的内容转换成路径即可。
        时间上不会超过两次扫描(一次是进栈得到简化路径,一次是出栈获得最后结果),所以时间复杂度是O(n),空间上是栈的大小,也是O(n)。*/
        Stack<String> stack=new Stack<String>();
        int start=0;
        for(int i=0;i<=path.length();i++){//注意i的范围。针对这种输入"/home",需要读取到最后一位,所以i可以为len
            if(i<path.length()&&path.charAt(i)!=‘/‘){continue;}
            if(start<i){//注意判断
                 String temp=path.substring(start,i);
                if(temp.equals("..")){
                    if(!stack.empty())//注意判断,不为空才可以弹出
                      stack.pop();
                }else{
                    if(temp.equals(".")){
                        start=i+1;//注意更新start为下一个不为/‘的索引
                        continue;
                    }
                    else{
                        stack.push(temp);
                    }
                }

            }
            start=i+1;
        }
        StringBuilder res=new StringBuilder();
        if(stack.empty()) res.append("/");//注意考虑栈空的情况
        while(!stack.empty()){
            String temp=stack.pop();
            res.insert(0,"/"+temp);//头插

        }
        return res.toString();
    }
}
时间: 2024-10-03 06:22:32

[leedcode 71] Simplify Path的相关文章

LeetCode开心刷题四十八天——71. Simplify Path

71. Simplify Path Medium 5101348FavoriteShare Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file system, a period . refers to the current directory. Furthermore, a do

71. Simplify Path做题报告

题目链接: 71. Simplify Path 题目大意: 简化路径,如果对于‘.'字符,表明为此目录,对于‘..'字符,表明为回到此目录上一节点(即:删除上一节点),我们现在需要删除多余的'/'字符和'.'字符 做题报告: (1)该题涉及的算法与数据结构与知识点 Java NIO中的Files类,正则表达式,栈 (2)自己的解答思路+代码+分析时间和空间复杂度 栈 class Solution { public String simplifyPath(String path) { String

leetcode 71 Simplify Path

题目连接 https://leetcode.com/problems/simplify-path/ Simplify Path Description Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider

71. Simplify Path (Stack)

Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" class Solution { public: string simplifyPath(string path) { stack<char

71. Simplify Path QuestionEditorial Solution

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" path = "/a/./b/../c/", => "/a/c" path = "/a/./b

leetcode 71 Simplify Path ------ java

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case where p

【一天一道LeetCode】#71. Simplify Path

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", =

[LeetCode] 71. Simplify Path 简化路径

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" Corner Cases: Did you consider the case where path = "/../"?In th