文章目录
20. Valid Parentheses
Given a string
s
containing just the characters'('
,')'
,'{'
,'}'
,'['
and']'
, determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
class Solution:
def isValid(self, s: str) -> bool:
stack = []
tab = {
')':'(',
']':'[',
'}':'{'
}
for x in s:
if x in tab:
if not stack:
return False
if tab[x] != stack.pop():
return False
else:
stack.append(x)
return not stack
- list for stack
- O(n), O(n)
=================================================
1047. Remove All Adjacent Duplicates In String
You are given a string
s
consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them. We repeatedly make duplicate removals ons
until we no longer can. Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Example 1:
Input: s = “abbaca”
Output: “ca”
Explanation:
For example, in “abbaca” we could remove “bb” since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is “aaca”, of which only “aa” is possible, so the final string is “ca”.
class Solution:
def removeDuplicates(self, s: str) -> str:
stack = [s[0]]
for i in range(1,len(s)):
if stack and s[i] == stack[-1]:
stack.pop()
else:
stack.append(s[i])
return "".join(stack)
- O(n), O(n)
150. Evaluate Reverse Polish Notation
You are given an array of strings
tokens
that represents an arithmetic expression in a Reverse Polish Notation.Evaluate the expression. Return an integer that represents the value of the expression.
Note that:
- The valid operators are
'+'
,'-'
,'*'
, and'/'
.- Each operand may be an integer or another expression.
- The division between two integers always truncates toward zero.
- There will not be any division by zero.
- The input represents a valid arithmetic expression in a reverse polish notation.
- The answer and all the intermediate calculations can be represented in a 32-bit integer.
Example 1:
Input: tokens = ["2","1","+","3","*"] Output: 9 Explanation: ((2 + 1) * 3) = 9
- Sol1:
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = list()
for x in tokens:
if x not in "+-*/":
stack.append(x)
else:
r = int(stack.pop())
l = int(stack.pop())
if x=='+':
stack.append(l+r)
if x=='-':
stack.append(l-r)
if x=='*':
stack.append(l*r)
if x=='/':
stack.append(int(l/r))
return int(stack.pop())
- Sol 2:
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
op_set = {'+', '-', '*', '/'}
def operate(a:str,b:str,op:str) -> str:
a,b = int(a), int(b)
if op == '+':
return str(a+b)
elif op == '-':
return str(a-b)
elif op == '*':
return str(a*b)
elif op == '/':
return str(int(a/b))
#return str(a//b) this is not correct for negative
stack = list()
for x in tokens:
#print(stack)
if x not in op_set:
stack.append(x)
else:
if stack:
b=stack.pop()
a=stack.pop()
stack.append(operate(a,b,x))
return int(stack.pop())
- O(n), O(n)