Problem Statement
Reverse Polish Notation (RPN) is a notation for writing arithmetic expressions that is famous for not needing parentheses. Perhaps best known for its use in certain calculators, RPN is also commonly used in virtual machines such as the JVM. The distinguishing feature of RPN is that arithmetic operators are written after their arguments. For example, the infix expression "3 - 4" would be written in RPN as "3 4 -" (all quotes for clarity only). Similarly, the infix expression "(3 - 4) * 5" would be written in RPN as "3 4 - 5 *". Notice how no parentheses were necessary in the RPN expression. No confusion is possible, because the infix expression "3 - (4 * 5)" would be written in RPN as "3 4 5 * -".
An RPN expression is evaluated using a stack. The expression is processed from left to right. Each number is pushed onto the stack as it is encountered. When an operator is encountered, the appropriate number of arguments are popped off the stack, the operation is performed, and the result is pushed back onto the stack. The final answer is the value on the top of the stack when the end of the expression is reached. The supported arithmetic operators are addition ('+'), subtraction ('-'), multiplication ('*'), and unary negation ('~')
For example, the RPN expression "2 3 + 6 ~ 7 * -" would be evaluated as shown in the following table:
Remaining Expression Stack New Stack 2 3 + 6 ~ 7 * - [] [2] 3 + 6 ~ 7 * - [2] [2 3] <---- Stacks grow to the right + 6 ~ 7 * - [2 3] [5] 6 ~ 7 * - [5] [5 6] ~ 7 * - [5 6] [5 -6] 7 * - [5 -6] [5 -6 7] * - [5 -6 7] [5 -42] - [5 -42] [47] <---- Final answer is 47
You will write a method that takes an RPN expression expr as a
An RPN expression is malformed if evaluating it would leave more than one value on the stack, or if evaluating it would cause some operator to try to pop an empty stack. Your method should return -999 if expr is malformed.
Definition
- Class:
- RPN
- Method:
- evaluate
- Parameters:
- String
- Returns:
- int
- Method signature:
- int evaluate(String expr)
- (be sure your method is public)
Constraints
- expr contains an odd number of characters between 1 and 33, inclusive.
- Every character in a (zero-based) odd position in expr is a space (' ').
- Every character in a (zero-based) even position in expr is either a digit ('0'-'9') or one of the characters '+', '-', '*', or '~'.
Examples
"2 3 + 6 ~ 7 * -"
Returns: 47
The example above.
"5 ~ ~ ~"
Returns: -5
Multiple negations are allowed.
"9 8 7 * * 4 5 - -"
Returns: 505
"1 + 2 + 3"
Returns: -999
"1 9 ~ 2 8 * +"
Returns: -999
"9 ~ 3 * 2 5 7 * +"
Returns: -999
"9 9 9 9 9 9 9 9 9 * * * * * * * *"
Returns: 387420489
The maximum answer.
"5"
Returns: 5
"6 7 +"
Returns: 13
"9 8 *"
Returns: 72
"3 4 -"
Returns: -1
"3 ~"
Returns: -3
"~ 7"
Returns: -999
"+ 1 2"
Returns: -999
"1 2 3 4 5 6 7 8 9 + + + + + + +"
Returns: -999
"5 0 + 3 0 * -"
Returns: 5
"9 ~ ~ 9 -"
Returns: 0
"9 9 9 9 9 9 9 9 * * * ~ * * * *"
Returns: -43046721
"7 ~ 7 9 + 9 9 8 3 - + * * 6 - +"
Returns: 2003
"8 ~ ~ ~ - ~ ~"
Returns: -999
"5 ~ 6 - 7 - 8 - ~ 9 +"
Returns: 35
"* 3 4"
Returns: -999
"2 8 - 9 5 + 0 0 + - 5 6 0 * - * -"
Returns: -76
"3 5 * 1 + 5 6 9 7 1 6 * * - - - -"
Returns: 50
"8 7 1 6 * 7 * + 2 + + 3 9 4 * * *"
Returns: 6372
"0 7 9 * 1 1 4 9 * * 7 - * + 2 + +"
Returns: 94
"6 2 4 * - 1 2 + 4 9 + 3 - 3 - + +"
Returns: 8
"5 0 0 9 1 * - 5 + 5 * * 5 * - 5 -"
Returns: 0
"5 7 3 5 + 8 6 5 + + + * 8 * + 5 +"
Returns: 1522
"8 4 9 4 + 4 * 3 + + 6 2 + 7 + + +"
Returns: 82
"6 ~ 4 - 2 3 4 ~ * ~ 4 - - 4 * ~ +"
Returns: 14
"2 3 - 4 6 9 7 2 8 - + - * - ~ ~ -"
Returns: 43
"9 4 8 5 ~ 4 * + - ~ + 7 + 4 5 + *"
Returns: 0
"2 1 9 + 9 ~ 5 4 4 ~ 5 - 3 4 7 7 4"
Returns: -999
"9 9 4 9 9 1 9 + 6 + + 0 3 - 1 5 9"
Returns: -999
"~ 8 3 3 8 8 6 ~ 0 9 3 8 4 0 - 9 3"
Returns: -999
"~"
Returns: -999
"+"
Returns: -999
"+ *"
Returns: -999
"0"
Returns: 0