Leetcode OJ : Evaluate Reverse Polish Notation Stack C++ solution
#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的更多相关文章
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- [LeetCode] Evaluate Reverse Polish Notation stack 栈
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [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 (Stack)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- hadoop Safe mode is ON 的解决办法
hadoop Safe mode is ON 的解决办法 搭了一个hadoop集群环境,近期总是出现读写文件错误的情况,查看name node的日志显示 (Safe mode is ON) Safe ...
- Extjs布局——layout: 'card'
先看下此布局的特性: 下面演示一个使用layout: 'card'布局的示例(从API copy过来的)——导航面板(注:导航面板切换下一个或上一个面板实际是导航面板的布局--layout调用指定的方 ...
- 在js中获取easyui datagrid的数据
可以在页面对datagrid的数据直接进行修改,然后提交到数据库,但是要求在提交前获取datagrid的所有行的数据.API提供了getData方法,但是怎么用了,没说. 最后这样写才搞定 var a ...
- GameAdmin
username:root e-mail :123@qq.com password:123
- tornado做简单socket服务器(TCP)
http://blog.csdn.net/chenggong2dm/article/details/9041181 服务器端代码如下: #! /usr/bin/env python #coding=u ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- loadrunner java协议脚本要点
常见问题 1. Error: Thread Context: Call to service of the driver failed, reason - thread context wasn't ...
- linux命令中 rpm –qa|grep softname的含义
rpm –qa是列出所有rpm包后面接管道 |grep softname就是查含有softname的包名
- 解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)
根据 src/corelib/global.h template <typename T>class QGlobalStatic{public: T *pointer; inline QG ...
- 函数(Functions)
概念: 下面是蛮经典的解释: What Good are Functions? You might have considered the situation where you would li ...