/**150. Evaluate Reverse Polish Notation * 1.1 by Mingyang * Here I began to think of the string into char and then to determine whether it is a number or a symbol, but I found the wrong! Because here string becomes char very difficult!!! * So Integer.parseint (tokens[i) can be a good solution to this problem, directly into an int, including your symbol!!!! */ Public intEVALRPN (string[] tokens) {Stack<Integer> stack =NewStack<integer>(); intA, B; for(inti = 0; i < tokens.length; i++) { if(Tokens[i].equals ("+") ) {a=Stack.pop (); b=Stack.pop (); Stack.push (b+a); } Else if(Tokens[i].equals ("-") ) {a=Stack.pop (); b=Stack.pop (); Stack.push (b-a); } Else if(Tokens[i].equals ("*") ) {a=Stack.pop (); b=Stack.pop (); Stack.push (b*a); } Else if(Tokens[i].equals ("/") ) {a=Stack.pop (); b=Stack.pop (); Stack.push (b/a); } Else{Stack.push (Integer.parseint (tokens[i])); } } returnStack.pop (); }
Evaluate Reverse Polish Notation