您的位置:首页 > 编程语言 > Python开发

Valid Parentheses @Leetcode -- Python

2014-05-08 16:16 309 查看
http://oj.leetcode.com/problems/valid-parentheses/

class Solution:
# @return a boolean
def isValid(self, s):
if s == '':
return True
left = '([{'
right = ')]}'
stack = []
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
# test
s = Solution()
print s.isValid('()')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: