LeetCode之Evaluate Reverse Polish Notation

原创 2015年07月10日 21:54:39
/*用栈模拟后缀表达式的求值过程。遍历字符串的容器:
1.遇到符号,将栈里面最上面的两个数字出栈进行符号表示的相应计算,并将结果入栈。
2.遇到数字,将数字入栈。
3.最后,栈顶元素即表达式的结果。*/
class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        if(tokens.empty()) return 0; 
        stack<int> nums;
        for(int i = 0; i < tokens.size(); ++i){
            if(tokens[i] == "+" || tokens[i] == "-"
            || tokens[i] == "*" || tokens[i] == "/"){
                int b = nums.top();
                nums.pop();
                int a = nums.top();
                nums.pop();
                if(tokens[i] == "+"){
                    a += b;
                    nums.push(a);
                }
                else if(tokens[i] == "-"){
                    a -= b;
                    nums.push(a);
                }
                else if(tokens[i] == "*"){
                    a *= b;
                    nums.push(a);
                }
                else if(tokens[i] == "/"){
                    a /= b;
                    nums.push(a);
                }
            }
            else nums.push(string2num(tokens[i]));
        }
        return nums.top();
    }
    
    int string2num(const string &s){
        int res(0);
        int sign(1);
        int i(0);
        while(i < s.size()){
            if(s[i] == '-') sign = -1;
            else if(s[i] == '+') sign = 1;
            else res = res * 10 + s[i] - '0';
            ++i;
        }
        return res * sign;
    }
};

leetcode_题解_Evaluate Reverse Polish Notation逆波兰式

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +,...

Evaluate Reverse Polish Notation【leetcode 2】

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -...

LeetCode–Evaluate Reverse Polish Notation

LeetCode–Evaluate Reverse Polish Notation (LeetCode - 评价逆波兰表示法,即后缀表达式) By X Wang (LeetCode: 一个在线编...

[leetcode]Evaluate Reverse Polish Notation题解

描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

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

题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators...

(leetcode)Evaluate Reverse Polish Notation

Evaluate Reverse Polish Notation 题目 Evaluate the value of an arithmetic expression in Reverse Pol...
  • fcunren
  • fcunren
  • 2014年08月18日 15:28
  • 282

leetcode笔记:Evaluate Reverse Polish Notation(逆波兰式的计算)

该题考查逆波兰式,也叫后缀表达式(将运算符写在操作数之后)。假设有一个表达式E,其后缀形式定义如下: 1. 如果E是一个变量或常量,则E的后缀式是E本身; 2. 如果E是E1 operator E...

LeetCode 150. Evaluate Reverse Polish Notation

LeetCode 150. Evaluate Reverse Polish Notation 栈

LeetCode|Evaluate Reverse Polish Notation

题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
  • hwb1992
  • hwb1992
  • 2014年04月20日 21:26
  • 616

LeetCode:《Evaluate Reverse Polish Notation》

#include #include #include #include using namespace std; class Solution { public: int evalRP...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode之Evaluate Reverse Polish Notation
举报原因:
原因补充:

(最多只允许输入30个字)