[C + +] leetcode:98 Evaluate Reverse Polish Notation

Source: Internet
Author: User

Topic:

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

valid operators Are + ,  -,  * ,  /. Each operand is an integer or another expression.

Some Examples:

  ["2", "1", "+", "3", "*")--((2 + 1) (3)-9  ["4", "", "5", "/", "+"], 4 + (13/5))

idea: solving inverse Polish expression: TheRPN expression consists of an operand (operand) and an operator (operator) that represents a precedence operation without parentheses, but with metacharacters, such as spaces.

The solution process, from left to right to iterate through the expression of each number and symbol, if the number is pressed into the stack, encountered is a symbol, the top of the stack is the stack, the first out of the stack for the operand two opt2, the second out of the stack for the operand of a opt1, the calculation opt1 operation symbol (+-*/, etc.) Opt2, And then press the calculation results into the stack. The only remaining element in the last stack is the result of the calculation. We construct an auxiliary function to perform the calculation.

Attention:

1. The stacks we maintain are stored in integers. Maintenance of the results of the calculation, before pressing the stack of the character type number conversion

Maintenance of a digital stack        stack<int> STK;
2. Remember again, string to int, Stoi function

Stk.push (Stoi (tokens[i));
3. Note that the RPN expression sequentially pops out of the two operands before and after the calculation order.

Encountered the operation symbol, the stack top two number pop out of the stack, the operation, the result of the operation into the stack                int op2 = Stk.top ();                Stk.pop ();                int OP1 = Stk.top ();                Stk.pop ();                Stk.push (OP (OP1, OP2, Tokens[i]));
Complexity: O (N)

AC Code:

Class Solution {Public:int EVALRPN (vector<string> &tokens) {if (tokens.size () = = 0) return 0;        Maintenance of a digital stack stack<int> STK;        Create a query table for an operational symbol set<string> Calcuset;        Calcuset.insert ("+");        Calcuset.insert ("-");        Calcuset.insert ("*");                Calcuset.insert ("/"); for (int i = 0; i < tokens.size (); i++) {//If not an operation symbol, press in the stack if (Calcuset.find (tokens[i]) = = CA            Lcuset.end ()) {Stk.push (Stoi (tokens[i)));                } else {//encountered operation symbol, the stack top two number pop out of the stack, the operation, the result of the stack int op2 = Stk.top ();                Stk.pop ();                int OP1 = Stk.top ();                Stk.pop ();            Stk.push (OP (OP1, OP2, Tokens[i]));    }} return Stk.top ();        } int op (int op1, int op2, string optor) {if (Optor = = "+") return OP1 + op2;  else if (Optor = = "-")          return OP1-OP2;        else if (Optor = = "*") return OP1 * OP2;    else return OP1/OP2; }};





[C + +] leetcode:98 Evaluate Reverse Polish Notation

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.