Evaluate Reversed Polish Notation

problem: [https://leetcode.com/problems/evaluate-reverse-polish-notation/ 150. Evaluate Reverse Polish Notation]

Problem description
Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are,  ,  ,. Each operand may be an integer or another expression.


 * Note
 * Division between two integers should truncate toward zero.
 * The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.


 * Example 1
 * Input:
 * Output: 9
 * Explanation:


 * Example 2
 * Input:
 * Output: 6
 * Explanation:


 * Example 3
 * Input:
 * Output: 22
 * Explanation:

Analysis
Given that all tokens and the whole input array is valid, in this problem we only encounter two types of tokens:
 * Integers: + or - sign (optional) followed by digits
 * Operators:

So we can use a iterator to go over the tokens from left to right.

Here are the things we do respectively when encountering each type of token:
 * 1) When encountering an integer, we save this integer for later use in computation.
 * 2) When encountering an operator, we perform the arithmetic operation using the two operands we have most recently encountered, and save the result for further computation.
 * 3) * This problem requires us to perform the computation operations from left to right in the order in which we receive the operators. There is no operator precedence issue to concern.


 * See the section for the procedure of checking the token type.
 * See the section for how to store the intermediate results.

When we have went over the last token, the result is obtained.

Checking token type
Since all tokens are valid, we can check the token type using the following method:
 * Check if one string is an integer:
 * An empty string is not an integer.
 * If we have the sign + or -, then check if index 1 is a digit.
 * Otherwise, check if index 0 is a digit.
 * All strings that are not integers are operators.

Data structure
We can keep a data stack to store the necessary integers in the intermediate steps of computation.

As we iterate over the input, we continuously modify the data stack.
 * When we encounter an integer, we push this integer onto the data stack.
 * When we encounter an operator:
 * Pop the last two integers from the data stack to use as operands ( and  ). Note that   is popped first, and then.
 * Compute the intermediate result (or the final result, if the data stack is empty at this moment) using the two operands and the operator.
 * Push this computed result onto the data stack. This result will be used in further computations.

At the end, the data stack will contain exactly one integer, which is our final result.

Time complexity

 * We go over each token exactly once. So the time to iterate over them is O(n).
 * Checking whether a token is an integer or an operator takes O(1) time.
 * Each computation takes O(1) time.
 * Popping from or pushing to the data stack takes O(1) time.

So the total time complexity is O(n).

Space complexity

 * Stack: What we need on stack:
 * Input string iterator
 * Two integers to serve as operands
 * Operator character
 * So the space complexity on stack is O(1).


 * Heap: We maintain a data stack on the heap. The space complexity for this is O(n), where n is the total number of input tokens.
 * The number of integers in the input data is one half (round up) of the total number of tokens.
 * Worst case example: . We need to store all integers into the data stack first. The data stack has its maximum size of O(n/2) = O(n).

So the total space complexity is O(n).

C++

 * String APIs:
 * To convert a string to an integer, use the function (provided by the  header).
 * To check if a character is a digit, use the function (provided by the  header).

Java

 * The  class:
 * Get character at index i: use the method.
 * The  class:
 * Check if a character is a digit: use the method.
 * The  class:
 * To convert a string to an integer: use the method.
 * The  class:
 * To implement a stack, we only insert / remove at right ends.