[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 ...
随机推荐
- 3123 高精度练习之超大整数乘法 - Wikioi
题目描述 Description 给出两个正整数A和B,计算A*B的值.保证A和B的位数不超过100000位. 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Ou ...
- EasyUI datagrid 改变url属性 实现动态加载数据
$(function () { //说明:btnsearch按钮,selCat下拉列表,ttdatagrid table $("#btnsearch").click(functio ...
- PAT-乙级-1013. 数素数 (20)
1013. 数素数 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 令Pi表示第i个素数.现任给两个正整 ...
- SQL注入中的WAF绕过技术
目录 1.大小写绕过 2.简单编码绕过 3.注释绕过 4.分隔重写绕过 5.Http参数污染(HPP) 6.使用逻辑运算符 or /and绕过 7.比较操作符替换 8.同功能函数替换 9.盲注无需or ...
- java基本对象Integer,String比较相等及equal案例说明
Integer i = new Integer(100); Integer i2 = new Integer(100); if(i == i2){ System.out.println("A ...
- 一个HexToInt的C/C++函数
int BetterVenca25(char* hex){ int res=0; for(;*hex;hex++) { int d=toupper(*hex); if(d & ...
- 使用FileZilla Server轻松搭建个人FTP服务器
Linux平台下快速搭建FTP服务器 服务器FTP Server环境搭建 针对以上遇到的问题的解决方案如下: 1)如何上传文件到云服务器上 关于这个问题,我首先想到的是使用FileZ ...
- Qt: 界面中使用中文(三种方法,QApplication::translate可指定编码)
界面中的字符串, 尽量的使用QObject::tr(text); 以便以后转换界面语言, 即使现在你还不考虑这个问题. 方法一: 每次设置时都使用: button->setText(QAppl ...
- 每个QWidget都有contentsMargins函数,善用QMargins
m_pSearchLineEdit = new QLineEdit(); QPushButton *pSearchButton = new QPushButton(this); pSearchButt ...
- Cinema 4D R16安装教程
CINEMA 4D_百度百科 http://baike.baidu.com/view/49453.htm?fr=aladdin 转自百度贴吧 [教程]Cinema 4D R16新功能介绍及安装教程_c ...