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
Parsing: the stack structure should be used to correctly parse the sequence of suffix expressions. It should be calculated in the same order as the original suffix expressions after scanning from left to right, otherwise, an error is returned!
public class solution {public int evalrpn (string [] tokens) {deque
stack = new arraydeque
(); For (INT I = 0; I
= '0' & tokens [I]. charat (0) <= '9') | (tokens [I]. charat (0) = '-' & tokens [I]. length ()> 1) {stack. push (integer. parseint (tokens [I]);} else {int x = stack. pop (); int y = stack. pop (); Switch (tokens [I]. charat (0) {Case '+': Stack. push (Y + x); break; Case '-': Stack. push (Y-x); break; Case '*': Stack. push (y * X); break; Case '/': Stack. push (y/X); break; default: Break ;}} return stack. pop () ;}