Give you a string path
, Indicates a point to a file or directory Unix style Absolute path ( With '/'
start ), Please translate it into a more concise specification path .
stay Unix Style file system , One point (.
) Represents the current directory itself ; Besides , Two points (..
) Means to switch the directory to the next level ( Point to the parent directory ); Both can be part of a complex relative path . Any number of consecutive slashes ( namely ,'//'
) Are treated as a single slash '/'
. For this problem , Points in any other format ( for example ,'...'
) Are considered documents / Directory name .
Please note that , Back to Canonical path The following format must be followed :
'/'
start .'/'
.'/'
ending .'.'
or '..'
).Return the simplified Canonical path .
Example 1:
Input :path = "/home/" Output :"/home" explain : Be careful , There is no slash after the last directory name .
Example 2:
Input :path = "/../" Output :"/" explain : It's not feasible to go up from the root , Because the root directory is the highest level you can reach .
Example 3:
Input :path = "/home//foo/" Output :"/home/foo" explain : In the canonical path , Multiple consecutive slashes need to be replaced with one slash .
Example 4:
Input :path = "/a/./b/../../c/" Output :"/c"
Tips :
1 <= path.length <= 3000
path
By the English letters , Numbers ,'.'
,'/'
or '_'
form .path
Is an effective Unix Style absolute path .class Solution(object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
result = []
plist = path.split('/')
for pos in plist:
if pos:
if pos == '..':
try:
result.pop()
except:
result = []
elif pos != '.':
result.append(pos)
return '/'+'/'.join(result)
# %%
s = Solution()
print(s.simplifyPath(path = "/home/"))