Lintcode:expression Evaluation (Basic Calculator III)

Source: Internet
Author: User

return Final  This   This question in a real interview?  2*6-(23+7)/(1+2), input is[  "2", "*", "6", "-", "(",  "+", "+", "7", ")", "/", c11/> ("," 1 "," + "," 2 ",")"],return 2+,-, *,/, (,).

This problem should actually be the basic Calculator III. Reference to the http://blog.csdn.net/nicaishibiantai/article/details/45740649

The idea is that there are two stacks, one stored number and one symbol. If a number is encountered, it is stored directly to the digital stack, and if a symbol is encountered, there are several cases:

1. The current symbol has a higher priority than the previous symbol, such as * above +, then directly into the stack

2. If the current symbol is lower than the previous, then all the existing symbols in the stack must be completed before the current symbol is pushed

3. The current symbol is "(", Direct push

4. The current symbol is ")", it is necessary to all "(" the previous symbol all finished

1  Public classSolution {2     /**3      * @paramExpression:an array of strings;4      * @return: An integer5      */6      Public intevaluateexpression (string[] expression) {7         //Write your code here8stack<integer> integers =NewStack<integer>();9stack<string> Ops =NewStack<string>();Ten         inti = 0; One          while(I <expression.length) { AString cur =Expression[i]; -             if(isOp (cur)) {//Current string was an op -                 if(Cur.equals ("(") ) Ops.push (cur); the                 Else if(Cur.equals (")")) { -                      while(Ops.size () >0 &&!ops.peek (). Equals ("(")) { - Integers.push (Calc (Integers.pop (), Integers.pop (), Ops.pop ())); -                     } + Ops.pop (); -                 } +                 Else{// +,-,*,/ A                      while(Ops.size () >0 &&precede (cur, ops.peek ())) { at Integers.push (Calc (Integers.pop (), Integers.pop (), Ops.pop ())); -                     } - Ops.push (cur); -                 } -             } -             ElseIntegers.push (integer.parseint (cur));//Current String is a integer, push to Integer stack ini++; -         } to          +          while(!Ops.isempty ()) { - Integers.push (Calc (Integers.pop (), Integers.pop (), Ops.pop ())); the         } *         returnIntegers.isempty ()? 0: Integers.pop (); $     }Panax Notoginseng      -      Public BooleanisOp (String input) { the         if(Input.equals ("+") | | input.equals ("-") | | input.equals ("*") +|| Input.equals ("/") | | Input.equals ("(") | | Input.equals (")")) A             return true; the         return false; +     } -      $      Public intCalcintAintB, String op) { $         if(Op.equals ("+"))returnA +b; -         Else if(Op.equals ("-"))returnB-A; -         Else if(Op.equals ("*"))returnA *b; the         Else returnb/A; -     }Wuyi      the      Public Booleanprecede (string A, string b) { -         if(B.equals ("*") | | b.equals ("/"))return true; Wu         if(B.equals ("+") | | b.equals ("-"))) { -             if(A.equals ("*") | | a.equals ("/"))return false; About             Else return true; $         } -         return false//case like (a+b) to the first + number, + and (than should return false -     } -};

Lintcode:expression Evaluation (Basic Calculator III)

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.