• 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;
        }
    };
  • 相关阅读:
    转--- 一些概念不错的理解
    python 生产者 --- 消费者
    python GUI 之 tkinter
    读DataSnap源代码(二)
    读DataSnap源代码(一)
    FireDAC探索 (二)
    FireDAC内部初探
    C++Builder XE7 中“匿名”方法实现
    DelphiXE7 Datasnap TDSClientCallbackChannelManager内部实现初探
    C++ Builder使用VC DLL
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759476.html
Copyright © 2020-2023  润新知