赞
踩
今天的三道题仍然是关于栈与队列,三道题都还算比较常规
直接贴代码咯
- class Solution:
- def isValid(self, s: str) -> bool:
- ## 使用栈和队列
- stack = []
- s = list(s)
- for i in s:
- if i == "{" or i == "[" or i == "(":
- stack.append(i)
- else:
- if len(stack) == 0:
- return False
- a = stack.pop()
- if i == "]" and a != "[":
- return False
- if i == "}" and a != "{":
- return False
- if i == ")" and a != "(":
- return False
- if len(stack) != 0:
- return False
- return True
-
-
-
-
- class Solution:
- def removeDuplicates(self, s: str) -> str:
- stack = []
- s = list(s)
- for i in s:
- if stack == [] or stack[-1] != i:
- stack.append(i)
- else:
- stack.pop()
- return "".join(stack)
- class Solution:
- def evalRPN(self, tokens: List[str]) -> int:
- stackNum = []
- for i in tokens:
- if i[0] == '-' and i[1:].isdigit():
- stackNum.append(i)
- elif i.isdigit():
- stackNum.append(i)
- else:
- temp1 = int(stackNum.pop())
- temp2 = int(stackNum.pop())
- if i == "+":
- stackNum.append(temp2+temp1)
- if i == "-":
- stackNum.append(temp2-temp1)
- if i == "*":
- stackNum.append(temp2*temp1)
- if i == "/":
- if temp2//temp1 < 0:
- if temp2//temp1 == temp2 / temp1:
- stackNum.append(temp2//temp1)
- else:
- stackNum.append(temp2//temp1 + 1)
- else:
- stackNum.append(temp2//temp1)
- return int(stackNum[0])
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。