Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators is + , - , * , / . Each operand is an integer or another expression.
Some Examples:
["2", "1", "+", "3", "*")--((2 + 1) (3)-9 ["4", "", "5", "/", "+"], 4 + (13/5))
It can be implemented using stacks. The operation symbol is the two-digit operation of the pop, otherwise push
Because the test case does not have an illegal expression, it is easy to accept
1 Public intEVALRPN (string[] tokens) {2Stack<string> st =NewStack<string>();3 for(inti=0;i<tokens.length;i++)4 {5String T =Tokens[i];6 if(T.equals ("+") | | t.equals ("-") | | t.equals ("*") | | t.equals ("/"))7 {8 intx2 =Integer.parseint (St.pop ());9 intX1 =Integer.parseint (St.pop ());Ten if(T.equals ("+")) OneSt.push (string.valueof (x1 +x2)); A Else if(T.equals ("-")) -St.push (string.valueof (x1-x2)); - Else if(T.equals ("*")) theSt.push (string.valueof (x1 *x2)); - Else -St.push (string.valueof (x1/x2)); - } + Else - St.push (t); + } A at returnInteger.parseint (St.pop ()); -}
[Leetcode] [JAVA] Evaluate Reverse Polish Notation