# Leetcode 150. Evaluate Reverse Polish Notation ## 題解 ```python! class Solution: def evalRPN(self, tokens: List[str]) -> int: stack = [] eval_chr = {'+','-','*','/'} for s in tokens: if s in eval_chr: n2 = stack[-1] stack.pop() n1 = stack[-1] # 後面的先 stack.pop() ans = 0 if s == "+": ans = str(int(n1) + int(n2)) elif s == "-": ans = str(int(n1) - int(n2)) elif s == "*": ans = str(int(n1) * int(n2)) elif s == "/": ans = str(int(int(n1) / int(n2))) stack.append(ans) else: stack.append(s) return int(stack[0]) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up