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:
'+', '-', '*', and '/'.Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation:
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].The Reverse Polish Notation (RPN) is a mathematical notation in which every operator follows all of its operands. This makes it very straightforward to evaluate using a stack, as stacks naturally follow the Last-In-First-Out (LIFO) principle.
Traverse the tokens:
By the end of the process, the stack should contain just one element - the result of the expression.
+, -, *, /), pop the top two operands from the stack, apply the operator, and push the result back onto the stack.