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> s;
for(int i=;i<tokens.size();i++){
string cur = tokens[i];
int temp = atoi(cur.c_str());
if(cur=="+"||cur=="-"||cur=="*"||cur=="/"){
int left,right;
if(!s.empty()){
right=s.top();
s.pop();
}
if(!s.empty()){
left=s.top();
s.pop();
}
if(cur=="+")
temp=left+right;
else if(cur=="-")
temp=left-right;
else if(cur=="*")
temp=left*right;
else if(cur=="/")
temp=left/right; }
s.push(temp);
}
int res = s.top();
return res;
}
};

evaluate-reverse-polish-notation——栈的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  2. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  3. 【LeetCode练习题】Evaluate Reverse Polish Notation

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

  4. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  5. 【LeetCode】150. Evaluate Reverse Polish Notation

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

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

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

  7. 150. Evaluate Reverse Polish Notation - LeetCode

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

  8. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  9. LeetCode: Evaluate Reverse Polish Notation 解题报告

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

  10. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

随机推荐

  1. leetcode刷题——查找

    知识点 备忘-必备算法 题目 顺序查找 二分查找 树表搜索 广度优先搜索算法(BFS) 深度优先搜索算法(DFS) 回溯(Backtracking) 题解 CS-Notes Algorithm_Int ...

  2. C#显示相机实时画面

    public partial class Form1 : Form { ICogAcqFifo mAcqFifo2;//定义一个相机对象 private ICogFrameGrabber mFrame ...

  3. 使用create datafile... as ...迁移数据文件到裸设备

    下面是一个测试过程 1.首先创建裸设备:root@ultra66 # cd /opt/app/oradata/test             root@ultra66 # lscontrol01.c ...

  4. NYOJ 293 Sticks

    Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 George took sticks of the same length and cut them r ...

  5. spring实战 — spring数据库事务

    欢迎加入程序员的世界,添物科技为您服务. 欢迎关注添物网的微信(微信号:tianwukeji),微博(weibo.com/91tianwu/),或下载添物APP,及时获取最新信息. 免费加入QQ群:5 ...

  6. BZOJ 1778 [Usaco2010 Hol]Dotp 驱逐猪猡 ——期望DP

    思路和BZOJ 博物馆很像. 同样是高斯消元 #include <map> #include <ctime> #include <cmath> #include & ...

  7. BZOJ2654 tree 【二分 + 最小生成树】

    题目 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. 输入格式 第一行V,E,need分别表示点数,边数和需要的白色边数. 接下来E行, ...

  8. 刷题总结——动态逆序对(bzoj3295)

    题目: Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素 ...

  9. dockerfile各种命令解析

    1.ADD命令,如果ADD的是压缩包,ADD之后会自动进行解压.....

  10. TYVJ4623 球球大作战·生存

    时间: 500ms / 空间: 65536KiB / Java类名: Main 背景 小天很喜欢玩球球大作战这个游戏,大家也应该都玩过.游戏规则是:移动自己的球,移动到别人的球(一定要比自己的球小)的 ...