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

150. Evaluate Reverse Polish Notation (Stack)的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  3. 【LeetCode】150. Evaluate Reverse Polish Notation

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

  4. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

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

  5. LeetCode OJ 150. Evaluate Reverse Polish Notation

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

  6. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  7. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

  8. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  9. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

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

随机推荐

  1. openssl 查看证书细节

    打印证书的过期时间 openssl x509 -in signed.crt -noout -dates 打印出证书的内容: openssl x509 -in cert.pem -noout -text ...

  2. FastAdmin 的 API 可以分级吗?

    FastAdmin 的 API 可以分级吗? 有小伙伴问 FastAdmin 的API 可以分别吗,使用 / 出现错误. Karson 的说明是: 完全支持的,默认是使用.进行分隔的,如果需要/,请开 ...

  3. Oracle instant client及pl sql developer的使用

    Oracle instant client的使用   最近重装了操作系统,使用的是Windows7 x64的版本,不准备安装Oracle,于是从官网上看到了Instant Client.   兴冲冲下 ...

  4. HDFS(二)

    HDFS的I/O主要是三个方面: 一致性 HDFS在一致性上面主要是通过校验和(checksum)来实现:从client发起写入的时候会校验一下文件内容,但是发生在pipeline的最后一个节点的时候 ...

  5. 数据库的备份与恢复(oracle 11g) (转)

    一.       内容与步骤 (注意这里许多步骤需要同学们查资料,理解并消化后才能完成) 1.数据库创建 (1)   安装Oralce11g: (2)   创建至少两个以上用户: (3)   每个用户 ...

  6. kbuild-(directory)

    -INDEX - this file: info on the kernel build process kbuild.txt - developer information on kbuild kc ...

  7. 写动态库时遇到了symbol lookup error问题

    之前写TLPI上的代码一直是手动进行错误处理,感觉代码冗余量很大,最后还是决定使用书上的tlph_hdr.h,顺便回顾下动态库的创建/使用. 参考很久之前的一篇博客 linux上静态库和动态库的编译和 ...

  8. 1102 Invert a Binary Tree

    题意:给定一个二叉树,要求输出翻转后的二叉树的层序序列和中序序列. 思路:不用真的翻转,只需要在输出时先访问右结点再访问左结点即可. 代码: #include <cstdio> #incl ...

  9. 导入城市文件数据(csv)格式demo

    页面: js: 后台:

  10. fir 窗口设计法

    加窗的原因.对于理想的低通滤波器H(exp(jw)),其h(n)是无限长序列.这是可以证明的.因此为了得到有限长的h(n)就需要截断,而这个过程就是加窗.由于h(n)截断即其频率响应就和理想的低通滤波 ...