[Leetcode] [150] Evaluate Reverse Polish Notation (Java)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.