[LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
题目描述:
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
题解:
所谓逆波兰式,即操作符位于操作数之后的表示法,我们常见的表示“三加四”都是表示成“3+4”,这是一种中缀表达式,换成逆波兰式,就应该表示成3 4 +,因此逆波兰式也称“后缀表达式”,具体的关于逆波兰式、中缀表达式、波兰式(即前缀表达式),参见维基百科。
很显然,具体操作符最近的两个数便是与这个操作符对应的操作数,用栈来实现是最直观的,这道题一道比较基础的栈的应用题,详情见代码:
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
#include <stack>
using namespace std; int evalRPN(vector<string> &tokens)
{
vector<string>::iterator iter;
stack<int> res;
int a,b;
for (iter=tokens.begin();iter!=tokens.end();iter++)
{
if(*iter=="+" || *iter=="-" || *iter=="*" || *iter=="/")
{
b = res.top();
res.pop();
a = res.top();
res.pop();
if(*iter=="+")
{
res.push(a+b);
}else if(*iter=="-")
{
res.push(a-b);
}else if(*iter=="*")
{
res.push(a*b);
}else if(*iter=="/")
{
res.push(a/b);
}
}
else
{
res.push(atoi((*iter).data()));
}
}
return res.top();
} int main()
{
freopen("in.in","r",stdin);
freopen("out.out","w",stdout); vector<string> tokens; string t;
while(!cin.eof())
{
cin>>t;
tokens.push_back(t);
} printf("%d\n",evalRPN(tokens));
return ;
}
[LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)的更多相关文章
- Evaluate Reverse Polish Notation(逆波兰式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java Evaluate Reverse Polish Notation(逆波兰式)
表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- 150. Evaluate Reverse Polish Notation(逆波兰表达式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如: ["2", "1&quo ...
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- 你不需要jQuery(五)
什么不用jQuery?因为它让你的网站体积变得臃肿.你的网站并不真的需要jQuery,不需要它带来的额外体积.带宽和加载时间. 用原生JavaScript简单实现jQuery提供的功能和方法 查找.选 ...
- java对象数组
问题描述: java 对象数组的使用 问题解决: 数组元素可以是任何类型(只要所有元素具有相同的类型) 数组元素可以是基本数据类型 数组元素也可以是类对象,称这样的数组为对象数组.在这种情况下 ...
- SQL 去除重复、获取最新记录
应用中常会有需要去除重复的记录,或者获取某些最新记录(如:每个用户可以答题多次,每次答题时间不同,现在要获取所有用户的最新答题记录,即每个用户取最新的一条) 使用group 和max 即可实现上述功能 ...
- 【概率】poj 2096:Collecting Bugs
Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other ...
- PAT-乙级-1013. 数素数 (20)
1013. 数素数 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 令Pi表示第i个素数.现任给两个正整 ...
- win8 优化笔记
win8可以关掉的服务: 以下是小编搜集的可以安全更改为手动启动的服务(按名称排序): Application Experience(启动时为程序处理应用程序兼容性缓存请求) ·Computer Br ...
- firefly的环境搭建(2013年9月25日最新,win下最详图文)
源地址:http://www.9miao.com/question-15-53785.html 一.安装PythonFirefly是采用Python编写的高性能.分布式游戏服务器框架,所以使用Fire ...
- VIM的高级使用
VIM的高级使用 转:http://www.cnblogs.com/itech/archive/2012/02/22/2363111.html 1)一些常用的Vim配置,在~/.vimrc中 syn ...
- linux命令中 rpm –qa|grep softname的含义
rpm –qa是列出所有rpm包后面接管道 |grep softname就是查含有softname的包名
- Java Web开发 之小张老师总结EL、JSP、Servlet变量
EL 11 JSP 9 Servlet JSP类别 pageContext pageContext * 作用域 pageScope pageContext.getAttribute() * reque ...