simplify_path
1# @leet start 2class Solution: 3 def simplifyPath(self, path: str) -> str: 4 """ 5 This question gives us an absolute path for a unix style file system, 6 which begins with a backslash and we need to transform it to a simplified 7 path, where '.' means the current dir, '..' means the parent dir, 8 consecutive slashes and treated as a single slash, and all other 9 chars are file/dir names. 10 11 We can solve this using a stack: 12 13 We solve this by splitting the path by '/' and then if we see a portion 14 with '..', we pop the top of the stack, if we see '.', or an empty 15 portion, we do nothing, since this means keep on the same portion, and 16 otherwise, append the portion to the stack. 17 18 At the end, we prepend a backslash and join the stack with backslashes 19 to get the final answer. 20 """ 21 stack = [] 22 for portion in path.split("/"): 23 if portion == "..": 24 if stack: 25 stack.pop() 26 if portion == "." or not portion: 27 continue 28 else: 29 stack.append(portion) 30 return "/" + "/".join(stack) 31 32 33# @leet end 34 35 36def test(): 37 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def simplifyPath(self, path: str) -> str: 5 """ 6 This question gives us an absolute path for a unix style file system, 7 which begins with a backslash and we need to transform it to a simplified 8 path, where '.' means the current dir, '..' means the parent dir, 9 consecutive slashes and treated as a single slash, and all other 10 chars are file/dir names. 11 12 We can solve this using a stack: 13 14 We solve this by splitting the path by '/' and then if we see a portion 15 with '..', we pop the top of the stack, if we see '.', or an empty 16 portion, we do nothing, since this means keep on the same portion, and 17 otherwise, append the portion to the stack. 18 19 At the end, we prepend a backslash and join the stack with backslashes 20 to get the final answer. 21 """ 22 stack = [] 23 for portion in path.split("/"): 24 if portion == "..": 25 if stack: 26 stack.pop() 27 if portion == "." or not portion: 28 continue 29 else: 30 stack.append(portion) 31 return "/" + "/".join(stack)
def
simplifyPath(self, path: str) -> str:
4 def simplifyPath(self, path: str) -> str: 5 """ 6 This question gives us an absolute path for a unix style file system, 7 which begins with a backslash and we need to transform it to a simplified 8 path, where '.' means the current dir, '..' means the parent dir, 9 consecutive slashes and treated as a single slash, and all other 10 chars are file/dir names. 11 12 We can solve this using a stack: 13 14 We solve this by splitting the path by '/' and then if we see a portion 15 with '..', we pop the top of the stack, if we see '.', or an empty 16 portion, we do nothing, since this means keep on the same portion, and 17 otherwise, append the portion to the stack. 18 19 At the end, we prepend a backslash and join the stack with backslashes 20 to get the final answer. 21 """ 22 stack = [] 23 for portion in path.split("/"): 24 if portion == "..": 25 if stack: 26 stack.pop() 27 if portion == "." or not portion: 28 continue 29 else: 30 stack.append(portion) 31 return "/" + "/".join(stack)
This question gives us an absolute path for a unix style file system, which begins with a backslash and we need to transform it to a simplified path, where '.' means the current dir, '..' means the parent dir, consecutive slashes and treated as a single slash, and all other chars are file/dir names.
We can solve this using a stack:
We solve this by splitting the path by '/' and then if we see a portion with '..', we pop the top of the stack, if we see '.', or an empty portion, we do nothing, since this means keep on the same portion, and otherwise, append the portion to the stack.
At the end, we prepend a backslash and join the stack with backslashes to get the final answer.
def
test():