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 +, -, ...
随机推荐
- 第一章 Spring整体框架和环境搭建
1.Spring 的整体架构 Spring框架主要由7大模块组成,它们提供了企业级开发需要的所有功能,而且每个模块都可以单独使用,也可以和其他模块组合使用,灵活且方便的部署可以使开发的程序更加简洁灵活 ...
- MapperScannerConfigurer(转)
转:http://blog.csdn.net/ranmudaofa/article/details/8508028 原文:http://www.cnblogs.com/daxin/p/3545040. ...
- Extjs布局——layout: 'card'
先看下此布局的特性: 下面演示一个使用layout: 'card'布局的示例(从API copy过来的)——导航面板(注:导航面板切换下一个或上一个面板实际是导航面板的布局--layout调用指定的方 ...
- uva10943
递推 还是比较容易的 /************************************************************************* > Author: ...
- 使用 Spark 进行微服务的实时性能分析
[编者按]当开发者从微服务架构获得敏捷时,观测整个系统的运行情况成为最大的痛点.在本文,IBM Research 展示了如何用 Spark 对微服务性能进行分析和统计,由 OneAPM 工程师编译整理 ...
- spring <form:checkboxes> tag and css class
I have issue with: <form:checkboxes path="roles" cssClass="checkbox" items=&q ...
- Windows下如何使用BOOST C++库 .
Windows下如何使用BOOST C++库 我采用的是VC8.0和boost_1_35_0.自己重新编译boost当然可以,但是我使用了 http://www.boostpro.com/produc ...
- jquery layout学习
1.官网:http://layout.jquery-dev.com/index.cfm 2.博客园:http://www.cnblogs.com/chen-fan/articles/2044556.h ...
- 深入理解JVM--JVM垃圾回收机制
Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放空间,既要写构造函数,又要写析构函数,很多时候都 ...
- Android Spinner(级联 天气预报)
activity_spinner.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...