Simplify Path @ LeetCode (Python)
kitt
posted @ 2014年2月20日 21:03
in LeetCode
, 1746 阅读
Use a stack.
class Solution: # @param path, a string # @return a string def simplifyPath(self, path): stack = ['/'] for i in path.strip('/').split('/'): if i == '.' or i == '': continue if i == '..': if len(stack) > 1: stack.pop() else: stack.append(i + '/') return ''.join(stack).rstrip('/') if len(stack) > 1 else ''.join(stack)