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
class Solution{
private:
bool isOperator(string s){
if(s.length()== && string("+-*/").find(s) != string::npos)
return true;
else
return false;
}
public:
int evalRPN(vector<string>& tokens){
stack<int> s;
for (string token : tokens){
if(!isOperator(token)){
s.push(stoi(token));
}else{
int y = s.top();
s.pop();
int x = s.top();
s.pop();
if(token == "+"){
s.push(x+y);
}else if(token == "-"){
s.push(x-y);
}else if(token == "*"){
s.push(x*y);
}else if(token == "/"){
s.push(x/y);
}
}
}
return s.top();
}
};

Evaluate Reverse Polish Notation的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  2. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  3. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  4. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  5. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  6. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

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

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

  9. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  10. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. Hadoop MapReduceV2(Yarn) 框架简介[转]

    对于业界的大数据存储及分布式处理系统来说,Hadoop 是耳熟能详的卓越开源分布式文件存储及处理框架,对于 Hadoop 框架的介绍在此不再累述,读者可参考 Hadoop 官方简介.使用和学习过老 H ...

  2. [Js]缓冲运动

    一.运动框架 1.在开始运动时,关闭已有定时器(否则会不断有新的定时器执行) 2.把运动和停止隔开(if/else) 二.缓冲运动 逐渐变慢,最后停止(距离越远速度越大) 速度=(目标值-当前值)/缩 ...

  3. 用Navicat for MYsql创建表,插入中文显乱码

    段都有编码设置.出现乱码肯定是你现在用的编码混乱造成的 解决办法: 第一步 先改数据库编码 先修改你的数据库,如果你页面用的是UTF-8编码那么你数据库内的编码也需要设置为UTF-8,每个字段都需要设 ...

  4. 事件函数SetEvent、PulseEvent与WaitForSingleObject详解

    系统核心对象中的Event事件对象,在进程.线程间同步的时候是比较常用,发现它有两个出发函数,一个是SetEvent,还有一个PulseEvent, 两者的区别是: SetEvent为设置事件对象为有 ...

  5. 图像显示与加载——opencv(转)

    cvLoadImage() 函数:IplImage* cvLoadImage("图像名称",参数): 函数作用:加载图片: 函数返回值:为IplImage结构体: 参数说明:参数值 ...

  6. IE和火狐 差异

    1.JavaScript中 1)IE和FireFox中childNodes的差别: <head> <script type="text/javascript"&g ...

  7. jQuery dialog 简介

    dialog是jQuery UI 库的一个UI组件,所以使用dialog时,不仅要引入jQuery.js(因为它只是轻量级的基础框架),还需要引入jQueryUI的js及相关css文件 示例: < ...

  8. SharePoint 2013 配置我的网站 图文引导

    博客地址:http://blog.csdn.net/FoxDave 本篇我们来讲述一下关于SharePoint中我的网站(My Sites)相关的东西. 我的网站是SharePoint 2013中面向 ...

  9. python解无忧公主数学题108

    """ python解无忧公主数学题108回文.py 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==& ...

  10. VS2013开发Android App 环境搭建

    下载并安装vs2013,(安装时发现多了with blend,百度后有人说是设计师用版本,这是不对的,害我花费不少时间查找程序员用版本).我安装的是Microsoft Visual Studio Ul ...