Leetcode-basic Calculator

Source: Internet
Author: User

1. Title Description:

The main idea is to implement an expression that evaluates to addition, subtraction, and parentheses, and all the numbers in the expression are positive integers.

2. Problem-Solving ideas:

This problem is actually a simplified version of the expression evaluation, you can use the classic operator precedence method to solve, that is, the establishment of operators stack and the operand stack, and then perform various in and out of the stack operation. At the same time this problem has only the plus, minus and parentheses three operators of the characteristics of the operator let us do some simplification of the precedence method.

First of all, in the simplification of operator precedence, the operator precedence relationship can be summed up as the closing parenthesis greater than plus minus sign greater than the opening parenthesis, and the plus minus sign priority is greater than the previous plus minus sign. This relatively simple operator precedence relationship allows us to store precedence relationships without a second-order matrix or by writing a precede function specifically. Notice ') ' because the priority will never go into the stack, we can store ' (' in the operator stack as 0, ' + ' is stored as 1, '-' stored as-1. The benefit of this simplification is that the code is reduced, and the code is more concise when adding and subtracting operations.

In addition, in order to let the operator stack always have something, so as not to judge whether the stack is empty or throw an unknown bug, we have to evaluate the expression as a whole with a pair of parentheses, so that does not change the value of the expression, but also saves a lot of trouble.

3. Specific code:

The whole idea of this problem is the operator precedence method, the specific explanation of some algorithmic ideas is reflected in the following code comment, the following code is really leetcode the code submitted on the platform.

classSolution { Public:    intCalculatestrings) {s= S +")"; Stack<int>optr; Stack<int>OPND; Optr.push (0); intTemp1 =0; intTemp2 =0; inttemp =0;  for(inti =0; I < s.length (); i++)        {            if(S[i] <='9'&& S[i] >='0')//Operand Count Stack{Temp= S[i]-'0';  while(I! = (S.length ()-1) && s[i+1] <='9'&& s[i+1] >='0')//handling Multi-digit situations{i++; Temp= Temp *Ten+ S[i]-'0';            } opnd.push (temp); }            Else            {                Switch(S[i]) { Case '('://the left parenthesis has the lowest priority, directly into the stackOptr.push (0);  Break;  Case '+'://plus minus, if the operation Fu Yi top is plus minus, then the stack and then into the stack, or directly into the stack                    if(Optr.top ()! =0) {Temp1=Opnd.top ();                        Opnd.pop (); Temp2=Opnd.top ();                        Opnd.pop (); Temp= Temp2 + optr.top () *Temp1;                        Opnd.push (temp);                    Optr.pop (); } Optr.push (1);  Break;  Case '-':                    if(Optr.top ()! =0) {Temp1=Opnd.top ();                        Opnd.pop (); Temp2=Opnd.top ();                        Opnd.pop (); Temp= Temp2 + optr.top () *Temp1;                        Opnd.push (temp);                    Optr.pop (); } Optr.push (-1);  Break;  Case ')'://meet the closing parenthesis until you meet the left parenthesis                     while(1)                    {                        if(Optr.top () = =0) {optr.pop ();  Break; }                        Else{Temp1=Opnd.top ();                            Opnd.pop (); Temp2=Opnd.top ();                            Opnd.pop (); Temp= Temp2 + optr.top () *Temp1;                            Opnd.push (temp);                        Optr.pop (); }                    }                     Break; default:                     Break; }            }        }        returnOpnd.top ();//the top of the last operand stack is the expression value    }};

Leetcode-basic Calculator

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.