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 逆波兰式,不用说,肯定考虑栈。
主要是题目所给的是字符串的数组,需要多次进行数字到字符串或者字符串到数字的转换,具体实现参考我的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(逆波兰式)的更多相关文章
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- Java Evaluate Reverse Polish Notation(逆波兰式)
表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...
- 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 ...
- [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 +, -, ...
- 150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如: ["2", "1&quo ...
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
随机推荐
- oracle 安装,启动 ,plsql 连接
1.下载oracle 服务器端,正常安装,在选择桌面类或者是服务器类的时候选择服务器类. 2.下载oracle 客户端解压版 下载地址 链接:https://pan.baidu.com/s/1mi ...
- Scala基础篇-04 try表达式
1.try表达式 定义 try{} catch{} finally{} //例子 try{ Integer.parseInt("dog") }catch { }finally { ...
- 程序员的职业方向: 是-->技术?还是-->管理?
岁之后还能不能再做程序员....... 绝大多数程序员最终的职业目标可能都是CTO,但能做到CEO的人估计会比较少,也有一少部分人自己去创业去当老板,也有部分人转行了,当老板的人毕竟是少数,转行的人都 ...
- iOS定位--CoreLocation框架
CoreLocation框架的使用 // 首先导入头文件 #import <CoreLocation/CoreLocation.h> CoreLocation框架中所有数据类型的前缀都是C ...
- xamarin 学习笔记01-环境配置
1.安装AndroidSDK 参考 2.安装NDK NDK下载地址:http://dl.google.com/android/ndk/android-ndk-r10e-windows-x86_64.e ...
- C#反射的使用
1.先定义个类,编译成dll,用于调用 nameSpace Test{ public class Class1 { private string _name; private int _age; pu ...
- 异步编程when.js
when.js很小,压缩后只有数kb,gzip后的大小几乎可以忽略.在Node和浏览器环境里都可以使用when.js 首先,我们看一小段代码: var getData = function(callb ...
- $("[lay-id='demo'] tbody tr[data-index=0]") 查找某行layui table
$("[lay-id='demo'] tbody tr[data-index=0]")
- caffe数据读取
caffe的数据读取分为lmdb和 待清理,包括fast 这个一系列是怎么转换成lmdb数据的
- vue之props传值与单向数据流
(1)组件通信 父组件向子组件传递数据.这个正向传递数据的过程就是通过props来实现的. 两者区别:props中声明的数据与组件data函数return返回的数据的主要区别就是props来自父级,而 ...