Evaluate Reverse Polish Notation

简介: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /.

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

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

Some examples:

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

注意:一定要使用后出栈的操作数作为第一操作数,否则,对于没有对称操作的操作符会出错。

 C++实现代码:
#include<iostream>
#include<stack>
#include<vector>
#include<string>
#include<cstdlib>
using namespace std;

class Solution
{
public:
    int evalRPN(vector<string> &tokens)
    {
        stack<int> operand;
        int operand1;
        int operand2;
        if(tokens.empty())
            return 0;
        int i;
        for(i=0; i<(int)tokens.size(); i++)
        {
            if(tokens[i]=="+")
            {
                if(!operand.empty())
                {
                    operand1=operand.top();
                    operand.pop();
                }
                if(!operand.empty())
                {
                    operand2=operand.top();
                    operand.pop();
                }
                operand2+=operand1;
                operand.push(operand2);
            }
            else if(tokens[i]=="-")
            {
                if(!operand.empty())
                {
                    operand1=operand.top();
                    operand.pop();
                }
                if(!operand.empty())
                {
                    operand2=operand.top();
                    operand.pop();
                }
                operand2-=operand1;
                operand.push(operand2);
            }
            else if(tokens[i]=="*")
             {
                if(!operand.empty())
                {
                    operand1=operand.top();
                    operand.pop();
                }
                if(!operand.empty())
                {
                    operand2=operand.top();
                    operand.pop();
                }
                operand2*=operand1;
                operand.push(operand2);
            }
            else if(tokens[i]=="/")
             {
                if(!operand.empty())
                {
                    operand1=operand.top();
                    operand.pop();
                }
                if(!operand.empty())
                {
                    operand2=operand.top();
                    operand.pop();
                }
                operand2/=operand1;
                operand.push(operand2);
            }
            else
            {
                operand1=atoi(tokens[i].c_str());
                operand.push(operand1);
            }
        }
        return operand.top();
    }
};

int main()
{
    Solution s;
    vector<string> vec={"4","13","5","/","+"};
    cout<<s.evalRPN(vec)<<endl;
}

 

相关文章
|
3月前
|
Python
完美解决丨2. `TypeError: list indices must be integers or slices, not str`
完美解决丨2. `TypeError: list indices must be integers or slices, not str`
LeetCode 150. Evaluate Reverse Polish Notation
根据逆波兰表示法,求表达式的值。 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
28 0
LeetCode 150. Evaluate Reverse Polish Notation
|
Python
解决only integer scalar arrays can be converted to a scalar index
解决only integer scalar arrays can be converted to a scalar index
287 0
解决only integer scalar arrays can be converted to a scalar index
|
TensorFlow 算法框架/工具
成功解决AttributeError: module tensorflow.sets has no attribute intersection
成功解决AttributeError: module tensorflow.sets has no attribute intersection
成功解决AttributeError: module tensorflow.sets has no attribute intersection
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
97 0
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
96 0
|
索引 Python
成功解决TypeError: tuple indices must be integers or slices, not str
成功解决TypeError: tuple indices must be integers or slices, not str
LeetCode 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. 说明: 整数除法只保留整数部分。
1354 0
|
Python
lecture 1 isinstance()函数
isinstance()与 type()区别:  type()不会认为子类是一种父类类型,不考虑继承关系。  isinstance()会认为子类是一种父类类型,考虑继承关系。
1221 0