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> pathStack;
        pathStack.push(path[0]);
        for(int i =1; i<path.length(); i++)
        {
            if(path[i]==‘/‘)
            {
                if(pathStack.top() == ‘/‘) continue; //ignore second ‘/‘
                else if(pathStack.top()==‘.‘) //ignore ‘./‘
                {
                    pathStack.pop();
                }
                else
                {
                    pathStack.push(path[i]);
                }
            }
            else if(path[i]==‘.‘)
            {
                if(pathStack.top() != ‘.‘) {
                    pathStack.push(path[i]);
                    continue;
                }
                pathStack.pop(); //pop ‘/..‘中的第一个‘.‘
                if(pathStack.size()==1) continue;

                pathStack.pop(); //pop ‘/..‘中的‘/‘
                while(pathStack.top() != ‘/‘) pathStack.pop(); //pop ‘/..‘的上一个文件路径
                if(pathStack.size()>1){
                    pathStack.pop();
                }
            }
            else
            {
                pathStack.push(path[i]);
            }
        }
        while(pathStack.size()>1 && (pathStack.top()==‘/‘ || pathStack.top()==‘.‘)) pathStack.pop(); //path最后的‘.‘和‘/‘要去掉

        //stack中剩余的是要return的path
        if(pathStack.empty()) return "/";
        int len = pathStack.size();
        path = path.substr(0,len);
        while(!pathStack.empty())
        {
            path[--len]=pathStack.top();
            pathStack.pop();
        }
        return path;
    }
}
时间: 2024-12-11 11:10:08

71. Simplify Path (Stack)的相关文章

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 ------ 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(化简路径) 解题思路和方法

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

【一天一道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

[LC] 71. Simplify Path

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 double period .. moves the directory up a level.

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"

[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) { /*这道题目是Li