150. Evaluate Reverse Polish Notation

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

Valid operators are +-*/. Each operand may be an integer or another expression.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation: 
  ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
class Solution {
private:
    set<string> operators = {"+","-","*","/"};
    stack<int> sk_rpn;
public:
    int evalRPN(vector<string>& tokens) {
        if(!tokens.size())
            return 0;
        for(int i=0; i<tokens.size(); i++)
        {
            if(operators.find(tokens[i])==operators.end())
            {
                sk_rpn.push(atoi(tokens[i].c_str()));
            }else
            {
                    if(tokens[i].compare("+")==0)
                    {
                        int num2 = sk_rpn.top();
                        sk_rpn.pop();
                        int num1 = sk_rpn.top();
                        sk_rpn.pop();
                        int result = num1 + num2;
                        sk_rpn.push(result);
                    }else if(tokens[i].compare("-")==0)
                    {
                        int num2 = sk_rpn.top();
                        sk_rpn.pop();
                        int num1 = sk_rpn.top();
                        sk_rpn.pop();
                        int result = num1 - num2;
                        sk_rpn.push(result);
                    }else if(tokens[i].compare("*")==0)
                    {
                        int num2 = sk_rpn.top();
                        sk_rpn.pop();
                        int num1 = sk_rpn.top();
                        sk_rpn.pop();
                        int result = num1 * num2;
                        sk_rpn.push(result);
                    }else if(tokens[i].compare("/")==0)
                    {
                        int num2 = sk_rpn.top();
                        sk_rpn.pop();
                        int num1 = sk_rpn.top();
                        sk_rpn.pop();
                        int result = num1 / num2;
                        sk_rpn.push(result);
                    }
                
            }
        }
        int result = sk_rpn.top();
        sk_rpn.pop();
        return result;
    }
};


Polish notation

05-31

Problem DescriptionnReverse Polish notation (RPN) is a method for representing expressions in which the operator symbol is placed after the arguments being operated on. nPolish notation, in which the operator comes before the operands, was invented in the 1920s by the Polish mathematician Jan Lucasiewicz. nIn the late 1950s, Australian philosopher and computer scientist Charles L. Hamblin suggested placing the operator after the operands and hence created reverse polish notation. nnRPN has the property that brackets are not required to represent the order of evaluation or grouping of the terms. nRPN expressions are simply evaluated from left to right and this greatly simplifies the computation of the expression within computer programs. nAs an example, the arithmetic expression (3+4)*5 can be expressed in RPN as 3 4 + 5 *. nnReverse Polish notation, also known as postfix notation, contrasts with the infix notation of standard arithmetic expressions in which the operator symbol appears between the operands. So Polish notation just as prefix notation.nnNow, give you a string of standard arithmetic expressions, please tell me the Polish notation and the value of expressions.n nnInputnThere're have multi-case. Every case put in one line, the expressions just contain some positive integers(all less than 100, the number of integers less than 20), bi-operand operators(only have 3 kinds : +,-,*) and some brackets'(',')'.nyou can assume the expressions was valid.n nnOutputnEach case output the Polish notation in first line, and the result of expressions was output in second line.nall of the answers are no any spaces and blank line.the answer will be not exceed the 64-signed integer.n nnSample Inputn1+2-3*(4-5)n1+2*(3-4)-5*6n nnSample OutputnCase 1:n- + 1 2 * 3 - 4 5n6nCase 2:n- + 1 * 2 - 3 4 * 5 6n-31

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试