-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path59_evalRPN.py
More file actions
25 lines (25 loc) · 817 Bytes
/
59_evalRPN.py
File metadata and controls
25 lines (25 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'''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.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9'''
from typing import List
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack=[]
operators=['+','-','*','/']
for ch in tokens:
if ch in operators:
first=stack.pop()
second=stack.pop()
if ch=='+':stack.append(first+second)
if ch=='-':stack.append(second-first)
if ch=='*':stack.append(second*first)
if ch=='/':stack.append(int(second/first))
else:
stack.append(int(ch))
return stack[0]