很简单的一道题,定义一个栈保留操作数,遇操作符则弹出运算即可。

bool isOperator(string &op)
{
//注意用法
return op.size() == && string("+-*/").find(op) != string::npos;
}
int evalRPN(vector<string> &tokens)
{
stack<string> s;
for (auto token : tokens)
{
if (!isOperator(token))
{
//如果是操作数,则入栈
s.push(token);
}
else
{
//如果是操作符,则弹出操作数进行运算
int y = stoi(s.top());
s.pop();
int x = stoi(s.top());
s.pop();
if (token == "+")x += y;
if (token == "-")x -= y;
if (token == "*")x *= y;
if (token == "/")x /= y;
s.push(to_string(x));
}
}
return stoi(s.top());
}

Leetcode 之Evaluate Reverse Polish Notation(41)的更多相关文章

  1. 【leetcode】Evaluate Reverse Polish Notation

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

  2. leetcode - [2]Evaluate Reverse Polish Notation

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

  3. 【Leetcode】Evaluate Reverse Polish Notation JAVA

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

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

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

  5. 【leetcode】Evaluate Reverse Polish Notation(middle)

    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. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

随机推荐

  1. POJ1990:MooFest——题解

    http://poj.org/problem?id=1990 题目大意:定义一对在树轴上的点,每对点产生的值为两点权值最大值*两点距离,求点对值和. 显然n*n复杂度不行,我们需要用树状数组维护两个东 ...

  2. BZOJ1876:[SDOI2009]SuperGCD——C++高精度良心题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1876 Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数 ...

  3. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A模拟 B三分 C dfs D map

    A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. HDU1384 差分约束

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. CentOS 下安装Mplayer播放器(转载)

    一.准备工作 需要的安装包及下载地址:1.mplayer源代码包(MPlayer-1.0rc4.tar.bz2)下载:http://www.mplayerhq.hu/MPlayer/releases/ ...

  6. java removeAll和重写equals、hashcode引起的性能问题

    问题背景: 上周发现了一个spark job的执行时间从原来的10-15分钟延迟到了7个小时!wtf,这是出了什么事引起了这么大的性能问题!! 立马查看job的运行日志,发现多次运行都是在某一个固定的 ...

  7. ZooKeeper管理员指南(九)

    部署 这部分包含了部署ZooKeeper的信息和覆盖这些话题 系统要求 集群(多服务)安装 单服务和开发者安装 前两部分假定你对在例如数据中心的生产环境安装ZooKeeper有兴趣.最后一部分包含你在 ...

  8. DBA操作常用命令

    一.ORACLE的启动和关闭   1.在单机环境下   要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下   su - oracle      a.启动ORACLE系统   orac ...

  9. Vue.js -- 过滤器

    VueJs中的过滤器基础 过滤器是一个通过输入数据,能够及时对数据进行处理并返回一个数据结果的简单函数.Vue有很多很便利的过滤器,可以参考官方文档, http://cn.vuejs.org/api/ ...

  10. 【Atcoder】ARC084 Small Multiple

    [题意]求一个k的倍数使其数位和最小,输出数位和,k<=10^5. [算法]最短路 [题解]考虑极端情况数字是可能爆long long的(例如k*num=100...000),所以确定基本方向是 ...