Simplify Path
Question
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
Tags
- Stack
- String
Thought
Best way is to use stack and just follow the rule for UNIX path style and pay attention to the corner case.
Code
class Solution(object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
dir_list = path.split('/')
for i, directory in enumerate(dir_list):
if directory == '' or directory == '.':
del dir_list[i]
elif directory == '..':
del dir_list[i]
if i != 0:
del dir_list[i-1]
return '/'.join(dir_list)