simplify path

题目不难,主要要想到点子上去,就是根据“/”split,否则的话要处理的corner case太多,不太容易pass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class (object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
paths = path.split("/")
new_paths = []
for p in paths:
if p == "." or p =="":
continue
elif p == "..":
if new_paths:
new_paths.pop()
else:
new_paths.append(p)
return "/" + "/".join(new_paths)