71. Simplify Path
# Medium
Solution:
find the effective start
'/'find the effective end
'/', extract string between start and endif string =
'.', do nothing; if string ='..',path2.pop(); elsepath2.append(string)convert
path2list to a real path string
class Solution:
def simplifyPath(self, path: str) -> str:
i = 0
path2 = []
while i < len(path):
# find start '/', i == start'/'
while i < len(path) and path[i] == '/':
i += 1
if i == len(path):
break
# find the end '/', i == end'/'
tmp = ''
while i < len(path) and path[i] != '/':
tmp += path[i]
i += 1
# add to path2
if tmp == '..':
if len(path2) != 0:
path2.pop()
elif tmp == '.':
continue
else:
path2.append(tmp)
# convert path2 to canonical path
if len(path2) == 0: return '/'
c = ''
for i in range(len(path2)):
c = c + '/' + path2[i]
return cTime complexity = , space complexity =
Last updated
Was this helpful?