简化文件的绝对路径
示例1:
输入:"/a/./b/../../c/"
输出:"/c"
示例2:
输入:"/a/../../b/../c//.//"
输出:"/c"
示例3:
输入:"/a//b////c/d//././/.."
输出:"/a/b/c"
切换上级目录很巧妙
class Solution:
def simplifyPath(self, path: str) -> str:
tmp = path.split('/')
res = []
cnt = 0
for i in range(len(tmp)-1, -1, -1):
if tmp[i] == '' or tmp[i] == '.':
continue
elif tmp[i] == '..':
cnt += 1
elif cnt > 0:
cnt -= 1
else:
res.append(tmp[i])
return ('/' + '/'.join(res[::-1])) if res else '/'