注意栈不是一个数据结构,需要自己用list实现栈的逻辑
class Solution:
def isValid(self, s: str) -> bool:
mystack=[]
for i in s:
if i=='(' or i=='{' or i=='[':
mystack.append(i)
#print(i,mystack)
else:#等于右括号了
if(len(mystack)==0):return False
ans=mystack.pop()
if i==')' and ans=='(':continue
elif i=='}' and ans=='{':continue
elif i==']' and ans=='[':continue
else:
return False
if len(mystack)==0:
return True
else:
return False
class Solution:
def removeDuplicates(self, s: str) -> str:
mystack=[]
for i in range(len(s)):
#print(mystack)
if (len(mystack)==0):
mystack.append(s[i])
continue
match=mystack.pop()
if match==s[i]:continue
else:
mystack.append(match)
mystack.append(s[i])
return ''.join(mystack)
我的数据结构知识没丢啊 真厉害!!!!
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
mystack=[]
for i in tokens:
#print(mystack)
if not(i =='+' or i=='-' or i=='*' or i=='/'):# 等于算数
mystack.append(i)
else: #等于算符
num1=int(mystack.pop())
num2=int(mystack.pop())
if i=='+':
res=num2+num1
elif i=='-':
res=num2-num1
elif i=='*':
res=num2*num1
else:
res=num2/num1
mystack.append(res)
return int(mystack.pop())