String to Integer (atoi) @ LeetCode (Python)
Divide Two Integers @ LeetCode (Python)

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)

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter