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. 基于链式链表的栈链式存储的C风格实现

    链式链表的头文件与CPP文件见前文 头文件: #ifndef _LINKSTACK_H_ #define _LINKSTACK_H_ typedef void LinkStack; //创建一个栈 L ...

  2. Luogu 5017 NOIP2018普及组T3 摆渡车 (斜率优化 + 必要的转移进行剪枝)

    题意: 有 n 名同学要乘坐摆渡车从人大附中前往人民大学,第 i 位同学在第 ti​ 分钟去 等车.只有一辆摆渡车在工作,但摆渡车容量可以视为无限大.摆渡车从人大附中出发. 把车上的同学送到人民大学. ...

  3. android 之 Dialog

    Android平台下对话框主要有普通对话框.选项对话框.单选多选对话框.进度对话框.日期对话框.时间对话框等. 在程序中通过开发回调方法onCreateDialog来完成对话框的创建,该方法需要传入代 ...

  4. 菜鸡的2017CPPC网络赛

    Friend-Graph Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. 2017 Multi-University Training Contest - Team 4

    日常绝望系列 Questionnaire HDU - 6075 In order to get better results in official ACM/ICPC contests, the te ...

  6. Windows同步阿里云时间

    Ctrl+R打开cmd命令框 输入:gpedit.msc 计算机配置”—“管理模版”—“系统”—“Windows 时间服务”—“时间提供程序”—“配置 Windows NTP 客户端 双击打开配置 W ...

  7. NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!

                                         Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...

  8. Spoj-VISIBLEBOX Decreasing Number of Visible Box

    Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman ...

  9. Python脚本实现单据体背景色及字段前景色设置

    #PythonEntitySetColor clr.AddReference('Kingdee.BOS.Core') from Kingdee.BOS.Core.DynamicForm.PlugIn. ...

  10. C Looooops(poj 2115)

    大致题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. 解题思路: 题意不难理解,只是利用 ...