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 debug源码完整版

    第一步:现在myeclipse或者eclipse中下载jad插件,将class文件翻译成java文件 点击下载安装 第二步:创建一个java工程,导出成jar包.jdk自带的jar包不包含debug ...

  2. ASP.NET Excel下载方法一览

    方法一 通过GridView(简评:方法比较简单,但是只适合生成格式简单的Excel,且无法保留VBA代码),页面无刷新 aspx.cs部分 using System; using System.Co ...

  3. Java锁,真的有这么复杂吗?

    为什么使用synchronizedvolatile,在多线程下可以保证变量的可见性,但是不能保证原子性,下面一段代码说明: 运行上面代码,会发现输出flag的值不是理想中10000,虽然volatil ...

  4. cesium primitive方式 ————http://blog.sina.com.cn/s/blog_15e866bbe0102y0ji.html

    Cesium学习笔记-工具篇17-PrimitivePoint自定义渲染-点 (2018-08-28 16:12:06) 转载▼ 标签: cesium primitive 自定义渲染 shader c ...

  5. 防止asp.net连续点击按钮重复提交

    1.在Page_Load中添加如下代码: protected void Page_Load(object sender, EventArgs e) { this.btnEdit.Attributes[ ...

  6. oracle char 多位,引发的问题

    我在表字有一字段type 类型为char(1),一开始用的还好,后来,char(1)不够用了,于是将char(1),变为char(2). 我做了一个字典表,来对应type,可是,总是查不到数据,最后, ...

  7. xingo的demo部署

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

  8. docker centos7 配置和宿主机同网段IP

    docker centos7 配置和宿主机同网段IP 1.安装brctl 命令 # yum -y install bridge-utils 2.编辑网卡配置文件 # vi ifcfg-eno16777 ...

  9. Linux从入门到适应(二):更换清华源

    1 进入到/etc/apt文件夹当中,找到sources.list,将其备份.命令:cp -p sources.list sources.list.old 2 采用管理员方式打开sources.lis ...

  10. vue-cli webpack 快速搭建项目

    一.安装vue npm install vue -g 二.用vue-cli快速搭建项目 //全局安装vue-cli npm install install -g vue-cli //创建一个基于web ...