For example, path =
"/home/", => "/home"
path = "/a/./b/../../c/", => "/c"Corner Cases:
- Did you consider the case where path =
"/../"?
In this case, you should return"/". - Another corner case is the path might contain multiple slashes
'/'together, such as"/home//foo/".
In this case, you should ignore redundant slashes and return"/home/foo".
Soultion:
class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
folders = path.split('/')
used = []
for i in range(0, len(folders)):
if folders[i]=='.':
used.append(False)
elif folders[i]=='..':
used.append(False)
for j in range(i-1,-1,-1):
if used[j]:
used[j] = False
break
elif len(folders[i])==0:
used.append(False)
else:
used.append(True)
solution = []
for i in range(0, len(folders)):
if used[i]:
solution.append('/')
solution.append(folders[i])
if len(solution) == 0:
solution.append('/')
return ''.join(solution)
No comments :
Post a Comment