class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
感觉是递归,最起码,压栈
"""
list_re=[]
for i in s:
if i=='('or i=='['or i=='{':
list_re.append(i)
elif i==')':
try:
if list_re[-1]=='(':
list_re.pop()
else:
return False
except:
return False
elif i == ']':
try:
if list_re[-1] == '[':
list_re.pop()
else:
return False
except:
return False
elif i == '}':
try:
if list_re[-1] == '{':
list_re.pop()
else:
return False
except:
return False
else:
pass
if list_re==[]:
return True
else:
return False
简单的压栈题
# coding=utf-8
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
感觉是递归,最起码,压栈
"""
list_re=[]
dict_sign={')':'(',']':'[','}':'{'}
for i in s:
if i=='('or i=='['or i=='{':
list_re.append(i)
elif i==')'or i==']'or i=='}':
try:
if list_re[-1]==dict_sign[i]:
list_re.pop()
else:
return False
except:
return False
if list_re==[]:
return True
else:
return False
建个字典,容易很多