Evaluate Reverse Polish Notation

Medium

Topics
ArrayStackMath

Evaluate an arithmetic expression in Reverse Polish Notation given as a list of tokens. Valid operators are + - * /; division truncates toward zero.

Example 1

Input:  tokens = ["2","1","+","3","*"]
Output: 9

Example 2

Input:  tokens = ["4","13","5","/","+"]
Output: 6

Constraints

  • 1 <= tokens.length <= 10^4
  • Tokens are integers or one of + - * /.
Run ⌘' · Submit ⌘⏎