【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation
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
使用栈(Stack),如果是操作数则压栈,如果是操作符则弹出栈顶两个元素进行相应运算之后再压栈。
最后栈中剩余元素即计算结果。
注意:操作数的顺序。
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> stk;
for(int i = ; i < tokens.size(); i ++)
{
if(tokens[i] == "+")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b+a);
}
else if(tokens[i] == "-")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b-a);
}
else if(tokens[i] == "*")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b*a);
}
else if(tokens[i] == "/")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(b/a);
}
else
{
int digit = atoi(tokens[i].c_str());
stk.push(digit);
}
}
return stk.top();
}
};

【LeetCode】150. Evaluate Reverse Polish Notation的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- [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逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
随机推荐
- 编译打包工具sbt的镜像设置
sbt可以和maven共用一个镜像,公司内部有的自然最后不过 创建文件:~/.sbt/repositories [repositories] local aliyun: http://maven.al ...
- Informatica 常用组件Lookup之五 转换属性
查找转换的属性标识数据库源.PowerCenter 如何处理转换,以及它如何处理高速缓存和多项匹配. 创建映射时,为每个查找转换指定属性.创建会话时,您可在会话属性中覆盖某些属性,如每个转换的索引和数 ...
- Merge Interval leetcode java
题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6] ...
- 系统出现bootmgr is missing解决方式,戴尔dellserver装系统须要特别注意的问题
系统出现bootmgr is missing解决方式,戴尔dellserver装系统须要特别注意的问题 欢迎关注http://blog.csdn.net/aaa123524457 转载请注明出处: h ...
- SqlServer2008安装时系统配置检查器重新启动计算机失败
系统:win7 64 旗舰版 软件版本:sqlserver 2008 在安装前执行:系统配置检查器 的时候报错 ‘重新启动计算机失败’,如下图 处理办法: RebootRequiredCheck ...
- java 读取本地的json文件
首先,要先去下载相关的jar包,否则你是无法做到的. 在百度或者谷歌里面输入java json jar包下载就行了(共7个包). xom-1.1.jar ezmorph-1.0.6.jar json ...
- 《Java程序设计》第16周周四:GUI编程及文件对话框的使用
第一部分:实验项目 项目一:文件及选择 目的:了解文件对话框的用法,熟悉GUI中事件驱动的核心步骤. 1. 阅读博文 "JFileChooser (Java Swing提供的文件选择对话框 ...
- Eclipse.ini參数设置(Maven Integration for Eclipse JDK Warning)
安装EclipseMaven插件后,Eclipse启动问题:Maven Integration for Eclipse JDK Warning. 解决方法: 1. 设置Eclipse使用的JRE为本 ...
- Windows 8提升普通管理员权限为超级管理员权限以及激活超级管理员Administrator
在Windows 8下运行某些操作时(比如删除一些文件或者更改某些系统设置时)系统会提示我们权限不够.须要Administrator账户的权限. 相信很多其它的使用者都会觉得自己当前使用的账户已经是系 ...
- MVC4.0网站发布
一.VS2010下MVC4.0项目的发布 首先,生成网站发布文件. 第一步,"右击"要发布的MVC4.0项目,选择"发布(B)..."选项,如图: 第二步,在& ...