Leetcode 71. Simplify Path Linux路径简化

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. For more information, see: Absolute path vs relative path in Linux/Unix

Note that the returned canonical path must always begin with a slash /, and there must be only a single slash / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the canonical path must be the shortest string representing the absolute path.

Example 1:

Input: "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.

Example 2:

Input: "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.

Example 3:

Input: "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.

Example 4:

Input: "/a/./b/../../c/"
Output: "/c"

Example 5:

Input: "/a/../../b/../c//.//"
Output: "/c"

Example 6:

Input: "/a//b////c/d//././/.."
Output: "/a/b/c"

题目链接:https://leetcode.com/problems/simplify-path/

我刚开始的想法:没有想到路径里可以包含 ...,所以我是一个一个字符处理,用例过了

240 / 254 test cases passed.

 最后我屈服了,这种方法行不通,但是代码留下,这个代码思路很奇特好吧

class Solution {
public:
    string simplifyPath(string path) {
       stack<char> stk;
        string res="";
        int len=path.length();
        for(int i=0;i<len;i++)
        {
          //cout<<"he";
            if(path[i]=='/')
            {
                if(!stk.empty()&&stk.top()=='/')//使用top(),一定要确认stk是否为空
                {
                    
                }
                else
                {
                    stk.push('/');
                }
            }
            else
            {
                    if(path[i]=='.')
                {
                    if(stk.top()=='.')
                    {
                        while(stk.top()!='/')
                        {
                            stk.pop();
                        }
                        stk.pop();
                        if(stk.empty())
                        {
                            stk.push('/');
                        }
                    }
                    else
                    {
                        while(stk.top()!='/')
                        {
                            stk.pop();
                        }
                        stk.pop();
                        if(stk.empty())
                        {
                            stk.push('/');
                        }
                    }
                }
                else
                {
                    stk.push(path[i]);
                }              
            }
        }
        if(stk.size()!=1&&stk.top()=='/')
            stk.pop();
        while(!stk.empty())
        {
            res=stk.top()+res;
            stk.pop();
        }
        return res;
    }
};

贴上正确的代码

class Solution {
public:
    string simplifyPath(string path) {
    istringstream ss(path);
        string tempPath;
        vector<string> stack;
        while(getline(ss,tempPath,'/')){
            if(tempPath == "." || tempPath.empty()){
                continue;
            }else if(tempPath == ".."){
                if(stack.empty() == false)
                    stack.pop_back();
            }else{
                stack.push_back(tempPath);
            }
        }
        
        string result;
        for(auto &s : stack){
            result += "/" + s;
        }
        
        return result.empty() ? "/" : result;
    }
};

猜你喜欢

转载自blog.csdn.net/salmonwilliam/article/details/86562812