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)), 6
Problem Solving Ideas:
To calculate the inverse Polish expression, the classic application of stack, the Java implementation is as follows:
public int EVALRPN (string[] tokens) {if (tokens = = NULL | | tokens.length = = 0) return 0; stack<integer> stack = new stack<integer> (); for (String Str:tokens) {if (Str.length () > 1 | | Character.isdigit (Str.charat (0))) Stack.push (Integer.parseint (str)), else {int a = Stack.pop (); int b = Stack.pop (); if ( Str.equals ("+")) Stack.push (b + a), else if (str.equals ("-")) Stack.push (b-a), Else if (str.equals ("*")) Stack.push (b * a); else if (str.equals ("/")) Stack.push (b/a);}} return Stack.pop ();}
Java for Leetcode Evaluate Reverse Polish Notation