leetcode.150. Evaluate Reverse Polish Notation
来源:互联网 发布:it招标 编辑:程序博客网 时间:2024/04/28 22:36
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) { int result = 0; int i; stack<int> opd; //存储操作数 int size = tokens.size(); for(i=0;i<size;i++) { if(tokens[i]=="*") { int rOpd = opd.top(); //右操作数 opd.pop(); int lOpd = opd.top(); //左操作数 opd.pop(); result = lOpd*rOpd; opd.push(result); } else if(tokens[i]=="/") { int rOpd = opd.top(); opd.pop(); int lOpd = opd.top(); opd.pop(); result = lOpd/rOpd; opd.push(result); } else if(tokens[i]=="+") { int rOpd = opd.top(); opd.pop(); int lOpd = opd.top(); opd.pop(); result = lOpd+rOpd; opd.push(result); } else if(tokens[i]=="-") { int rOpd = opd.top(); opd.pop(); int lOpd = opd.top(); opd.pop(); result = lOpd-rOpd; opd.push(result); } else { opd.push(atoi(tokens[i].c_str())); } } return opd.top(); }};
0 0
- LeetCode 150. Evaluate Reverse Polish Notation
- [leetcode] 150.Evaluate Reverse Polish Notation
- 【leetcode】150. Evaluate Reverse Polish Notation
- Leetcode 150. Evaluate Reverse Polish Notation
- [LeetCode]150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation LeetCode
- Leetcode 150. Evaluate Reverse Polish Notation
- leetcode 150. Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation
- LeetCode *** 150. Evaluate Reverse Polish Notation
- leetcode-150. Evaluate Reverse Polish Notation
- LeetCode-150.Evaluate Reverse Polish Notation
- leetcode.150. Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation
- [leetcode] 150. Evaluate Reverse Polish Notation
- [leetcode] 【栈】150. Evaluate Reverse Polish Notation
- LeetCode - 150. Evaluate Reverse Polish Notation
- [LeetCode] 150. Evaluate Reverse Polish Notation
- 2014第五届蓝桥杯C/C++程序设计本科B组决赛
- 在没有activity的时候使用getContentResolver方法
- 应用性能优化
- java的String类中的null和isEmpty()的区别
- 全文索引----中文分词器mmseg4j
- leetcode.150. Evaluate Reverse Polish Notation
- The police and cooker 2
- acm_十进制转二进制
- 前端项目实战(梅兰商城)
- UDP通讯实例
- Linux 64位下一键安装scipy等科学计算环境
- 设置父视图的透明度不该变子视图的透明度
- 局部敏感哈希LSH原作者的论文和程序LSH Algorithm and Implementation (E2LSH)
- 第十周第十一周项目4教师兼干部类