Question: https://oj.leetcode.com/problems/simplify-path/
Question Name: Simplify Path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Solution: # @param path, a string # @return a string def simplifyPath(self, path): path = path.split("/") stack = [] for token in path: # Useless token. May be duplicate "/" or the leading # and tailing pseudo token. if token == "": continue # Keep in current directory elif token == ".": continue # Move to father directory, if there is elif token == "..": if len(stack) != 0: stack.pop() else: continue # Move the a specific son directory else: stack.append(token) return "/" + "/".join(stack) |