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. 有关linux的GPG签名验证错误的解决方法。

    GPG签名验证错误:由于没有公钥,下列签名无法进行验证: NO_PUBKEY 6AF0E1940624A220 找了下原因,虽然不知道原理,不过大概意思还是能才出来的,解决方法如下: gpg --ke ...

  2. (转)Duplicate Symbol链接错的原因总结和解决方法

    duplicate symbol是一种常见的链接错误,不像编译错误那样可以直接定位到问题的所在.但是经过一段时间的总结,发现这种错误总是有一些规律可以找的.例如,我们有如下的最简单的两个类代码: // ...

  3. iOS设置UINavigationBar 的样式

    为了方便演示,我用storyBoard建立了一个基本的导航栏 并在代码中获得了NavgationBar UINavigationBar *bar = self.navigationController ...

  4. JQuery中如何处理键盘事件

    背景:比如在页面中,敲击回车键以后,实现登录功能,在之前的Winform开发时,也遇到过处理键盘事件的功能,比如游戏软件中,上下左右的移动等. 代码如下: $(document).keydown(fu ...

  5. HDU 3480 DP 斜率优化 Division

    把n个数分成m段,每段的值为(MAX - MIN)2,求所能划分得到的最小值. 依然是先从小到大排个序,定义状态d(j, i)表示把前i个数划分成j段,所得到的最小值,则有状态转移方程: d(j, i ...

  6. 【05】project board

    GitHub 上的 project board 我总是用 Jira 做大项目,独立项目用 Trello,这两者我都很喜欢. 后来我知道,GitHub 也有类似的 project board: 我个人为 ...

  7. CSS里总算是有了一种简单的垂直居中布局的方法了

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. Debian7配置LAMP(Apache/MySQL/PHP)环境及搭建建站

    完整Debian7配置LAMP(Apache/MySQL/PHP)环境及搭建建站 第一.安装和配置Apache Web服务器 运行升级命令来确保我们的系统组件各方面都是最新的. apt-get upd ...

  9. 雅图CAD

    今天培训了雅图CAD. 由辅助线确定下一步的位置,是个好思想.

  10. 11g自动分区超过最大限制

    公司业务系统一张表按时间每天分区 写入数据时报错:ORA-14300: 分区关键字映射到超出允许的最大分区数的分区 ORA-14300: partitioning key maps to a part ...