Length of Last Word (Python)
Subsets II @ LeetCode (Python)

Valid Parentheses @ LeetCode (Python)

kitt posted @ 2014年2月13日 01:58 in LeetCode , 4545 阅读

 

class Solution:
    # @return a boolean
    def isValid(self, s):
        if s == '': return True
        stack = []
        left = '([{'; right = ')]}'
        for i in s:
            if i == '(' or i == '[' or i == '{':
                stack.append(i); continue
            for j in xrange(3):
                if i == right[j]:
                    if not stack or stack[-1] != left[j]:
                        return False
                    else:
                        stack.pop(); continue
        return not stack

登录 *


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