71. Simplify Path
给定一个unix上面的路径字符串,尝试简化之:其中".."代表上一层,"."代表当前路径,连续的多个"/"可以看做一个"/",特别的是"/../"的结果是"/"
""" 71. Simplify Path Medium 319 964 Favorite Share Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" path = "/a/../../b/../c//.//", => "/c" path = "/a//b////c/d//././/..", => "/a/b/c" In a UNIX-style file system, a period ('.') refers to the current directory, so it can be ignored in a simplified path. Additionally, a double period ("..") moves up a directory, so it cancels out whatever the last directory was. For more information, look here: https://en.wikipedia.org/wiki/Path_(computing)#Unix_style 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: def pushDir(self, pathlist, dirname): """ 将dirname加入路径列表中 """ if dirname == "" or dirname==".": pass elif dirname == "..": if pathlist: pathlist.pop() else: pathlist.append(dirname) def simplifyPath(self, path): """ :type path: str :rtype: str """ pathlist = [] dirname = "" for i in path: if i != "/": dirname += i else: self.pushDir(pathlist, dirname) dirname = "" self.pushDir(pathlist, dirname) return "/"+"/".join(pathlist)