Leetcode Note: Evaluate Reverse Polish Notation (inverse Polish calculation)
I. Description
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +,-, *,/. Each operand may be an integer or another expression.
Some examples:
[2, 1, +, 3, *] -> ((2 + 1) * 3) -> 9
[4, 13, 5, /, +] -> (4 + (13 / 5)) -> 6
Ii. Question Analysis
This question examines the inverse Polish formula, also called the suffix expression (the operator is written after the operand ). Suppose there is an expression E whose suffix format is defined as follows:
If E is a variable or constant, the suffix of E is E itself. If E is an expression in the form of E1 operator E2, how is the operator binary operator, then E is suffixed with E1, E2,
Operator; if E is an expression in the form of (E1), the suffix of E1 is the suffix of E.
An example is as follows:
The following uses(a+b)*c
For example:
(a+b)*c
The inverse polish type isab+c*
Assume that the computerab+c*
Push the elements from left to right to the stack. Then, based on the operator, the two elements at the top of the stack are pushed out of the stack, and the operation is executed.ab+c*
The execution result is as follows:
A inbound stack (0 position); B inbound stack (1 position); operator encountered
“+”
, Set
a
And
b
Output stack, execute
a+b
To obtain the result.
d=a+b
, And then
d
Inbound stack (0 position); c inbound stack (1 position); operator encountered
“*”
, Set
d
And
c
Output stack, execute
d*c
To obtain the result.
e
, And then
e
Inbound stack (0 position ).
After the above calculation, the computer can obtain(a+b)*c
Calculation Resulte
.
It is very easy to implement the inverse Polish formula, which can be done using stacks. In a program, you must convert integers and strings.
Iii. Sample Code
# Include
# Include
# Include
Using namespace std; class Solution {public: int str2int (string s) // convert string to int {int result = 0; int base = 1; int t = 1; // if (s [0] = '-') t =-1; for (int I = s. size ()-1; I> = 0; -- I) {if (s [I]> = '0' & s [I] <= '9 ') {result + = base * (s [I]-'0'); base * = 10 ;}} return result * t;} int evalRPN (vector
& Tokens) {stack
K; for (int I = 0; I <tokens. size (); ++ I) {if (tokens [I] = + | tokens [I] =-| tokens [I] = * | tokens [I] = /) {int Num2 = k. top (); // The first retrieved is the right operand k. pop (); int Num1 = k. top (); // left operand k. pop (); if (tokens [I] = +) {k. push (Num1 + Num2);} else if (tokens [I] =-) {k. push (Num1-Num2);} else if (tokens [I] = *) {k. push (Num1 * Num2);} else if (tokens [I] ==/) {k. push (Num1/Num2) ;}} else k. push (str2int (tokens [I]);} return k. top (); // The last element left in the stack is the result }};
Iv. Summary
Although the entire idea is simple, there are still some details during programming, such as two operands popped up from the stack, the first is the right operand, and the second is the left operand; whether to convert string to int. You may need to consider other data types, such as floating point numbers and other boundary conditions...