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:
思路:
代码:
class Solution:
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
lpath = [v for v in path.split("/") if v != "." and v != ""]
res = []
for i in range(len(lpath)):
if lpath[i] == '..':
if i-1 >= 0:
res = res[:-1]
else:
res.append(lpath[i])
return '/'+'/'.join(res)