• 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 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".
    class Solution {
    private:
        int index;
        string path;
        string getpath()
        {
            index++;
            string newpath="";
            while(index<path.length() && path[index]!='/')
            {
                newpath=newpath+path[index];
                index++;            
            }
            return newpath;
        }
    public:
        string simplifyPath(string path) 
        {
            this->path=path;
            stack<string> stk;
            index=0;
            while(index<path.size())
            {
                string newpath=getpath();
                if(newpath=="..")
                {
                    if(stk.empty()==false)
                        stk.pop();
                    continue;
                }
                if(newpath=="." || newpath==""continue;
                stk.push(newpath);
            }
            string result="";
            while(stk.empty()==false)
            {
                result='/'+stk.top()+result;
                stk.pop();
            }
            if(result.length()==0) result="/";
            return result;
        }
    };
  • 相关阅读:
    Java中的LinkedList
    Java中的List集合
    Java中的集合Collection
    Java中的异常
    mvc+EF实现简单的登陆功能
    ASP.NET MVC学习三-数据传递之模型绑定
    ASP.NET MVC学习二之 Controller
    ASP.NET MVC 学习一之路由
    ASP.NET MVC学习
    winform获取网页代码的两种方式:
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759476.html
Copyright © 2020-2023  润新知