其他分享
首页 > 其他分享> > leetcode [71] Simplify Path

leetcode [71] Simplify Path

作者:互联网

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"   题目大意: 找出简化过后的路径,.表示当前目录(可以忽略不计),..表示上一级目录,目录最后不能带/。   解法: 使用"/"作为划分符号,将path划分得到字符串数组(忽略'.'和空字符串),如果字符串数组中有"..",则将上一个字符串元素删除,最后将字符串使用"/"连接 C++:
class Solution {
public:
    vector<string> split(string path){
        int i=0,j=0;
        vector<string>tmp;
        while(i<path.size()&&path[i]=='/') i++;
        j=i;
        for(;j<path.size();j++){
            if(path[j]=='/'){
                if(j>i){
                    tmp.push_back(path.substr(i,j-i));
                    while(path[j]=='/') j++;
                    i=j;
                }
            }
        }
        if(path[path.size()-1]!='/'){
            int index=path.size()-1;
            for(;index>=0;index--) if(path[index]=='/') break;
            tmp.push_back(path.substr(index+1,path.size()-index));
        }

        return tmp;
    }
    string simplifyPath(string path) {
        vector<string>strs;
        vector<string>p=split(path);
        for(string str:p){
            if(str==".."){
                if(!strs.empty()) strs.erase(strs.end());
            }else if(str=="."){
                continue;
            }else{
                strs.push_back(str);
            }
        }
        string res="/";
        for(string s:strs){
            res=res+s+"/";
        }
        if(res.size()>1) res=res.substr(0,res.size()-1);

        return res;
    }
};

  太菜了,c++这个split函数实现了半天,还是python和java好,有自带的split函数。

Python:
class Solution(object):
    def simplifyPath(self, path):
        """
        :type path: str
        :rtype: str
        """
        places=[p for p in path.split("/") if p!="." and p!=""]
        stack=[]
        for p in places:
            if p=="..":
                if len(stack)>0:
                    stack.pop()
            else:
                stack.append(p)
        return "/"+"/".join(stack)
 

标签:index,string,..,res,Example,Simplify,71,path,leetcode
来源: https://www.cnblogs.com/xiaobaituyun/p/10627946.html