#define ADDITION  '+'
#define SUBSTRACTION '-'
#define MULTIPLICATION '*'
#define DIVISION '/' class Solution {
public:
set<char> tokenSet{'+', '-', '*', '/'};
stack<int> num;
int evalRPN(vector<string> &tokens) {
for (auto &token : tokens) {
auto iter = tokenSet.find( token[token.size()-] );
if ( iter == tokenSet.end() ) {
num.push( atoi( token.c_str() ) );
} else {
int right = num.top();
num.pop();
int left = num.top();
num.pop();
switch ( *iter )
{
case ADDITION :
num.push(left + right);
break;
case SUBSTRACTION:
num.push(left - right);
break;
case MULTIPLICATION:
num.push(left * right);
break;
case DIVISION:
num.push(left / right);
break;
default:
break;
}
}
}
int ret = num.top();
num.pop();
return ret;
}
};

Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution的更多相关文章

  1. leetcode - [2]Evaluate Reverse Polish Notation

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

  2. 【leetcode】Evaluate Reverse Polish Notation

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

  3. [LeetCode] Evaluate Reverse Polish Notation stack 栈

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

  4. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  5. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  6. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

  7. leetcode 150. Evaluate Reverse Polish Notation ------ java

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

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

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

  9. 150. Evaluate Reverse Polish Notation (Stack)

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

随机推荐

  1. 1196: [HNOI2006]公路修建问题 - BZOJ

    Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织 ...

  2. BZOJ 3288 Mato矩阵 解题报告

    这个题好神呀..Orz taorunz 有一个结论,这个结论感觉很优美: $$ans = \prod_{i=1}^{n}\varphi(i)$$ 至于为什么呢,大概是这样子的: 对于每个数字 $x$, ...

  3. 破解之寻找OEP[手动脱壳](1)

    OEP:(Original Entry Point),程序的入口点,软件加壳就是隐藏了OEP(或者用了假的OEP), 只要我们找到程序真正的OEP,就可以立刻脱壳. PUSHAD (压栈) 代表程序的 ...

  4. POJ2348+博弈

    /* 博弈 关键态:较大数是较小数的2倍以上. */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...

  5. GridBagLayout()的使用方法

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...

  6. JavaScript 获取客户端计算机硬件及系统信息

    1.浏览器信息 //浏览器信息 function BrowserInfo() { var userLanguage = navigator.userLanguage;     // 用户在自己的操作系 ...

  7. ArcGIS学习记录—ArcGIS ArcMap编辑状态中线打断的问题

    摘要:在处理数据时,我们经常会遇到线打断的问题,比如需要指定在线上某处打断线,或者新建网络数据集时需要在线的交点处打段线等等.现将桌面版中我所遇到的线打断的工具总结如下: 在ArcGIS矢量处理数据时 ...

  8. C#和.net之间的关系

    What is the difference between C# and .NET? In addition to what Andrew said, it is worth noting that ...

  9. centos 如何用 rsyslog 搭建本地日志服务(续1: omprog模块与php deamon的配合使用)

    上一篇说到了如何用 rsyslog 搭建本地的日志服务,地址在这里,没有看的童鞋可以先瞅一眼 : http://www.cnblogs.com/smallrookie/p/5677004.html 显 ...

  10. Codeforces Round #221 (Div. 2) C. Divisible by Seven(构造 大数除法 )

    上几次的一道cf题. 题目:http://codeforces.com/contest/376/problem/C 性质: (4)a与b的和除以c的余数(a.b两数除以c在没有余数的情况下除外),等于 ...