The title is here: https://leetcode.com/problems/evaluate-reverse-polish-notation/
The label Stack
"Problem analysis" thinking is not much to say, it is probably, encountered the number of such operand, the first pressure to the stack inside; encounter +,-, *,/words, you can perform a local operation, the top of the stack of two elements out and operator to perform the operation, calculated, and then pressed back to the stack, save the subsequent operation.
"A little bit of experience," the following is the good code of others, especially worth mentioning is, using the interface method, to simulate the function of lambda expression, using function as a parameter. And it also improves the extensibility of the code very well.
1 Public classSolution {2 3 /**Use interface as Work-around for lambda expression*/4 Public InterfaceOperator {5 intEvalintXinty);6 }7 8@SuppressWarnings ("Serial")9 Public Static Finalmap<string, operator> operatormap =Ten NewHashmap<string, solution.operator>(){{ OnePut ("+",NewOperator () { A Public intEvalintXintY) {returnX +y;}}); -Put ("-",NewOperator () { - Public intEvalintXintY) {returnX-y;}}); thePut ("*",NewOperator () { - Public intEvalintXintY) {returnX *y;}}); -Put ("/",NewOperator () { - Public intEvalintXintY) {returnX/y;}}); + }}; - + Public intEVALRPN (string[] tokens) { Astack<integer> operands =NewStack<integer>(); at for(String token:tokens) { - if(Operatormap.containskey (token)) { - //For operator:calculate Operation and then push to stack - intOP2 =Operands.pop (); - intOP1 =Operands.pop (); - Operands.push (Operatormap.get (token). Eval (OP1, OP2)); in}Else { - //For Operand:push to Stack to Operands.push (Integer.parseint (token)); + } - } the returnOperands.pop (); * } $ Panax Notoginseng}
[Leetcode] [150] Evaluate Reverse Polish Notation (Java)