[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 ...
随机推荐
- js根据id、pid把数据转为树结构
//格式化树数据 function toTreeData(data) { var pos = {}; var tree = []; var i = 0; while (data.length != 0 ...
- BZOJ 4036 [HAOI2015] Set 解题报告
首先我们不能一位一位的考虑,为什么呢? 你想想,你如果一位一位地考虑的话,那么最后就只有 $n$ 个数字,然而他给了你 $2^n$ 个数字,怎么看都不对劲呀.(我是因为这样子弄没过样例才明白的) 所以 ...
- 深入理解SQL注入绕过WAF与过滤机制
知己知彼,百战不殆 --孙子兵法 [目录] 0x0 前言 0x1 WAF的常见特征 0x2 绕过WAF的方法 0x3 SQLi Filter的实现及Evasion 0x4 延伸及测试向量示例 0x5 ...
- mysql数据库备份执行计划
为什么需要数据备份?如果数据库因为人为或其他不可控的因素导致数据库数据丢失或损坏,导致的后果将会非常严重. 为什么需要执行计划?备份操作如果每天人工管理的话,将会非常麻烦,需要借助工具来制定执行计划, ...
- linux fork函数与vfork函数
一.fork1. 调用方法#include <sys/types.h>#include <unistd.h> pid_t fork(void);正确返回:在父进程中返回子进程的 ...
- 2.Adding a Controller
MVC stands for model-view-controller. MVC is a pattern for developing applications that are well ar ...
- Spring MVC 与 web开发
转载:http://coderbee.net/index.php/java/20140719/959 项目组用了 Spring MVC 进行开发,觉得对里面的使用方式不是很满意,就想,如果是我来搭建开 ...
- CURL 多线程问题
http://blog.csdn.net/wslz2001/article/details/12117127 默认情况下libcurl完成一个任务以后,出于重用连接的考虑不会马上关闭 如果没有新的TC ...
- 利用CodeIgniter中的Email类发邮件
CodeIgniter拥有功能强大的Email类.以下为利用其发送邮件的代码. 关于CI的Email类的详情请参考:http://codeigniter.org.cn/user_guide/libra ...
- 1106. Two Teams(dfs 染色)
1106 结点染色 当前结点染为黑 朋友染为白 依次染下去 这题是为二分图打基础吧 #include <iostream> #include<cstdio> #include ...