美文网首页
Leetcode 71. Simplify Path

Leetcode 71. Simplify Path

作者: persistent100 | 来源:发表于2017-07-26 16:12 被阅读0次

    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 this case, you should return "/".
    Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo".

    简化Unix文件绝对路径,就是添加了许多.和/的无关字符。需要依次分析路径,遇到../向后回溯,注意判断以下几种特别情况。如果输入如以下序列:

    "////b"
    "/home/"
    "/a/./b/.."
    "/a/./b/../../c/../../c/a/d"
    "/.....hidden"
    "/..hidden"
    "/a/b/d../.././.../..../...hidd..en.."
    "/."
    "./.."
    "/../..."
    ".././a.."
    "..."
    

    那么输出为:

    "/b"
    "/home"
    "/a"
    "/c/a/d"
    "/.....hidden"
    "/..hidden"
    "/a/b/.../..../...hidd..en.."
    "/"
    "/"
    "/..."
    "/a.."
    "/"
    

    C 代码已通过,如下所示:

    char* simplifyPath(char* path) {
        int length=0,anslength=0;
        while(path[length]!='\0')
        {
            if(path[length]=='/')
            {
                path[anslength]=path[length];
                anslength++;
                break;
            }
            length++;
        }
        if(anslength==0)
        {
            path[0]='/';
            path[1]='\0';
            return path;
        }
        while(path[length]!='\0')
        {
            if(path[length]=='/')
            {
                if(path[anslength-1]!='/')
                {
                    path[anslength]=path[length];
                    anslength++;
                    length++;
                }
                else
                {
                    length++;
                }
            }
            else if(path[length]=='.')
            {
                if(path[length+1]=='\0'||(path[length+1]!='\0'&&path[length+1]=='/'))
                {
                    length++;
                }
                else if(path[length+1]!='\0'&&path[length+1]=='.'&&(path[length+2]=='\0'||(path[length+2]!='\0'&&path[length+2]=='/')))
                {
                    if(anslength>1)anslength--;
                    while(anslength>1&&path[anslength-1]!='/')
                        anslength--;
                    length=length+2;
                }
                else
                {
                    while(path[length]!='\0'&&path[length]!='/')
                    {
                        path[anslength]=path[length];
                        anslength++;
                        length++;
                    }
                }
            }
            else
            {
                while(path[length]!='\0'&&path[length]!='/')
                {
                    path[anslength]=path[length];
                    anslength++;
                    length++;
                }
            }
            //printf("%d %s\n",anslength,path);
        }
        //while(path[anslength-1]=='.')
       //     anslength--;
        if(anslength>1&&path[anslength-1]=='/')
            anslength--;
        path[anslength]='\0';
        return path;
    }
    

    相关文章

      网友评论

          本文标题:Leetcode 71. Simplify Path

          本文链接:https://www.haomeiwen.com/subject/qnkokxtx.html