Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:
- push numbers into stack and whenever operators comes pop top two items
- and operate on them and push the result.
- */
- class Solution:
- def evalRPN(self, tokens: List[str]) -> int:
- st=[]
- for i in tokens:
- if i.isdigit() or (i[1:].isdigit() and i[0]=='-'):
- st.append(int(i))
- else:
- a=st.pop()
- b=st.pop()
- if i=='+':
- st.append(a+b)
- elif i=='-':
- st.append(b-a)
- elif i=='*':
- st.append(a*b)
- else:
- st.append(int(b/a))
- return st[0]
Advertisement
Add Comment
Please, Sign In to add comment