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 逆波兰式,不用说,肯定考虑栈。

主要是题目所给的是字符串的数组,需要多次进行数字到字符串或者字符串到数字的转换,具体实现参考我的blog,整数转字符串,字符串转整数

这里我采用的是c++标准库sstream来实现转换。

代码:

class Solution {
private:
bool isSymbol(string a){
return a=="+"||a=="-"||a=="*"||a=="/";
}
int Evaluate(int a,int b,char c){
switch (c)
{
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
return a/b;
break;
default:
break;
}
}
public:
int evalRPN(vector<string> &tokens) {
stack<string> container;
for(int i=;i<tokens.size();++i){
if(isSymbol(tokens[i])&&!container.empty()){
string temp2Str=container.top();container.pop();
string temp1Str=container.top();container.pop();
int temp2;
int temp1;
stringstream s;
s<<temp2Str;s>>temp2;
s.clear();
s<<temp1Str;s>>temp1;
s.clear(); stringstream s2;
int res=Evaluate(temp1,temp2,tokens[i][]);
s2<<res;
string resStr=s2.str();
container.push(resStr);
}else{
container.push(tokens[i]);
}
}
stringstream s;
int result=;
string reultStr=container.top();
s<<reultStr;
s>>result;
return result;
}
};

Evaluate Reverse Polish Notation(逆波兰式)的更多相关文章

  1. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  2. Java Evaluate Reverse Polish Notation(逆波兰式)

    表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...

  3. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  4. lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

    题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...

  5. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  6. 150. Evaluate Reverse Polish Notation(逆波兰表达式)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. 150 Evaluate Reverse Polish Notation 逆波兰表达式求值

    求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如:  ["2", "1&quo ...

  8. Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值

    根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...

  9. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

随机推荐

  1. 策略模式--Java篇

    策略模式(Strategy):它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. 下面将以商场打折为例子,说明策略模式.商场收银如何促销,用打折还是 ...

  2. SQL——连接查询、聚合函数、开窗函数、分组功能、联合查询、子查询

    连接查询 inner join,用的最多,表示多张表一一对应 聚合函数 操作行数据,进行合并 sum.avg.count.max.min 开窗函数 将合并的数据分布到原表的每一行,相当于多出来了一列, ...

  3. vb,wps,excel 分裂

    Sub 分列() '以空格为分隔符,连续空格只算1个.对所选中的单元格进行处理 Dim m As Range, tmpStr As String, s As String Dim x As Integ ...

  4. OpenGL VAO, VBO 使用简介

    参照代码样例: // This function takes in a vertex, color, index and type array // And does the initializati ...

  5. iTOP-4412开发板-LinuxC-继电器模块的测试例程

    平台:iTOP-4412开发板 实现:继电器模块测试例程 继电器的 C 的测试程序,C 测试程序可以在 Android系统,Qt 系统以及最小 linux 系统上运行,文档以 Android 系统上测 ...

  6. linux 11201(11203) ASM RAC 安装

    注意:11G的RAC安装,如果升级,则会新建目录在放软件,原来的不删除,所以所需空间比较大. 1.安装系统,把所有的开发包全部安装上 关掉防火墙和SELinux yum -y install comp ...

  7. Centos7 安装MongoDB的详细过程

    一.简介 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品 ...

  8. linux crontab创建计划任务

    1.编辑计划任务 编辑crontab文件 crontab -e 2.查看计划任务日志 查看crontab日志 tail -100f /var/log/cron 3.创建计划任务格式 (1)基本格式 : ...

  9. mac 目录颜色设置

    1 export CLICOLOR=1  2 export LSCOLORS=gxfxaxdxcxegedabagacad

  10. xingo的demo部署

    1)安装go 2)安装git xingo默认使用google protobuf来进行序列化和反序列化操作,所以需要安装 go get  github.com/golang/protobuf/proto ...