zoukankan      html  css  js  c++  java
  • Simplify Path

    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) {
            string ret;
            stack<string> stackPath;
            int len = path.length();
            int start = 1;
            if(len <= 1) return path;
            for (int i = 1; i < len; i++) {
                if(path[i] == '/'){
                    string str = path.substr(start,i-start);
                    start = i+1;
                    if(str == ".."){
                        if(!stackPath.empty())
                            stackPath.pop();
                    }else if(str == "."){
                        continue;
                    }else if(str.length() > 0){
                        stackPath.push(str);
                    }
                }
            }
            if (start < len) {
                string str = path.substr(start, len - start);
                if (str == "..") {
                    if (!stackPath.empty())
                        stackPath.pop();
                } else if (str != ".") {
                    ret = "/" + str;
                }
            }
    
            while(!stackPath.empty()){
                ret = "/" + stackPath.top() + ret;
                stackPath.pop();
            }
            if(ret.length()==0) ret = "/";
             return ret;
        }
    };
  • 相关阅读:
    nginx uwsgi django
    ubuntu config proxy
    jdbc调用sparksql
    jdbc调用sparksql on yarn
    JDK错误
    JDK错误
    docker错误
    docker错误
    Django网站直接使用supervisor部署
    Django网站直接使用supervisor部署
  • 原文地址:https://www.cnblogs.com/wxquare/p/5872809.html
Copyright © 2011-2022 走看看