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 {
public:
int evalRPN(vector< string > &tokens) {
stack< int > operandStack;
int operand1;
int operand2;
for(int i = ; i < tokens.size(); i++){
if(tokens[i]=="+"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 += operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="-"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 -= operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="*"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 *= operand1;
operandStack.push(operand2);
}
else if(tokens[i]=="/"){
operand1 = operandStack.top();
operandStack.pop();
operand2 = operandStack.top();
operandStack.pop();
operand2 /= operand1;
operandStack.push(operand2);
}
else{
operand1 = atoi(tokens[i].c_str());
operandStack.push(operand1);
}
}
return operandStack.top();
}
};

150. Evaluate Reverse Polish Notation (Stack)的更多相关文章

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

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

  2. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  3. 【LeetCode】150. Evaluate Reverse Polish Notation

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

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

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

  5. LeetCode OJ 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. 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逆波兰表示法

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

随机推荐

  1. docker could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network

    原因: 无法进行网络分配了 解决方法: // 所有的网络 docker network ls // 删除不用的,腾出多余的 docker network rm <networkname> ...

  2. jekyll 安装使用

    1. 安装     条件: ruby gem 注意版本,同时建议使用国内的镜像 gem install jekyll bundler   2. 创建网站   jekyll new my-awesome ...

  3. quick3.3rc1导入工程到ADT

    1.libcocos2dx  路径到/quick-3.3rc1/cocos/platform/android/java这一层 2.quick项目 路径到D:\codeide\test11\framew ...

  4. 【转】刚发现一个linux在线文档库。很好很强大。

    原文网址:http://blog.csdn.net/longxibendi/article/details/6048231 1.网址: http://www.mjmwired.net 2.比如查看这个 ...

  5. Spark性能优化:资源调优篇(转)

    在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以及如何设置 ...

  6. mysql的partition分区

    前言:当一个表里面存储的数据特别多的时候,比如单个.myd数据都已经达到10G了的话,必然导致读取的效率很低,这个时候我们可以采用把数据分到几张表里面来解决问题.方式一:通过业务逻辑根据数据的大小通过 ...

  7. CSS3 教程 选择器 标记一下防止 要找时404

    客 » Airen的博客 CSS3 选择器——基本选择器 作者:大漠 日期:2011-08-09 点击:6418  CSS的选择器,我想大家并不会陌生吧,因为天天在使用,但对于CSS3的选择器,要运 ...

  8. 杂项:大数据 (巨量数据集合(IT行业术语))

    ylbtech-杂项:大数据 (巨量数据集合(IT行业术语)) 大数据(big data),指无法在一定时间范围内用常规软件工具进行捕捉.管理和处理的数据集合,是需要新处理模式才能具有更强的决策力.洞 ...

  9. 转:mysql加锁处理分析

    MySQL/InnoDB的加锁分析,一直是一个比较困难的话题.我在工作过程中,经常会有同事咨询这方面的问题.同时,微博上也经常会收到MySQL锁相关的私信,让我帮助解决一些死锁的问题.本文,准备就My ...

  10. MySql——查询题目练习

    本次查询我们基于这几张表查询 ***********查询练习********** 1. 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,cl ...