栈和队列_leetcode20

#coding=utf-8
# 解题思路:栈 20190302 找工作期间

class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []

for i in s :
if i == "(" or i == "{" or i == "[":
stack.append(i)
else:

if len(stack) == 0:
return False

else:
match = None
topItem = stack.pop()

if i == ')':
match = '('

if i == ']':
match = '['

if i == '}':
match = '{'


if topItem != match:
return False

if len(stack) > 0:
return False
else:
return True


s = Solution()

print s.isValid("()")
posted @ 2019-03-17 15:55  AceKo  阅读(106)  评论(0编辑  收藏  举报