150. Evaluate Reverse Polish Notation - LeetCode
Question
150. Evaluate Reverse Polish Notation
Solution
2 1 + 3 *
是((2+1)*3)
的后缀(postfix)或逆波兰(reverse Polish)记法,计算这个表达式容易想到栈,当见到一个数时就入栈,见到操作符时该运算符作用于从该栈中弹出的两个数上,将所得结果入栈。
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String tmp : tokens) {
if (tmp.length() > 1) {
stack.push(Integer.parseInt(tmp));
continue;
}
char c = tmp.charAt(0); // String转char
int a, b;
switch (c) {
case '+':
b = stack.pop();
a = stack.pop();
stack.push(a + b);
break;
case '-':
b = stack.pop();
a = stack.pop();
stack.push(a - b);
break;
case '*':
b = stack.pop();
a = stack.pop();
stack.push(a * b);
break;
case '/':
b = stack.pop();
a = stack.pop();
stack.push(a / b);
break;
default:
stack.push(c - '0'); // char 转 int
}
}
return stack.pop();
}
Reference
150. Evaluate Reverse Polish Notation - LeetCode的更多相关文章
- 【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】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode OJ 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 +, -, ...
- Evaluate Reverse Polish Notation——LeetCode
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- C语言函数中的3个点 ...有什么作用
标准库提供的一些参数的数目可以有变化的函数.例如我们很熟悉的printf,它需要有一个格式串,还应根据需要为它提供任意多个"其他参数".这种函数被称作"具有变长度参数表的 ...
- 一份你可以在 <head> 里设置的列表
A list of everything that could go in the <head> of your document github 原地址:https://github.co ...
- 微信小程序黑客马拉松即将开始,来做最酷的 Mini Program Creators!
微信小程序黑客马拉松正式启动 近日,小程序斩获一项世界级殊荣--作为一项全新的技术和应用创新,小程序首次获选世界互联网领先科技成果.目前小程序应用数量已超过 100 万,覆盖了 200 多个细分行业, ...
- C语言---魔方阵
魔方阵的定义:在n*n的方阵中,每一行的和=每一列的和=对角线的和.(本文中涉及的n为大于3的奇数). 例如3*3的魔方阵为: 5*5的魔方阵为: 如何写魔方阵呢? 1.数字1位于第一行的正中间2.下 ...
- TINY语言采用递归下降分析法编写语法分析程序
目录 自顶向下分析方法 TINY文法 消左提左.构造first follow 基本思想 python构造源码 运行结果 参考来源:聊聊编译原理(二) - 语法分析 自顶向下分析方法 自顶向下分析方法: ...
- numpy---(精简)
numpy get started 导入numpy库, 并查看版本 import numpy as np np.__version__ '1.14.3' # pyplot显示画图, 数据分析与可视化 ...
- [转]Fabric2.3中使用test-network搭建测试网络
这个测试网络一方面可以用来学习Fabric,另一方面也可以让一些更有经验的开发者来测试他们的智能合约和应用,但是不建议用于生产环境,在2.0版本后,这个测试网络也取代了原来的"first-n ...
- drf中的请求与响应
请求与响应(3星) 请求:Request REST framework 传入视图的request对象不再是Django默认的HttpRequest对象,而是REST framework提供的扩展了Ht ...
- java_类的访问控制符
1.分类: public protected default private是java中的访问控制修饰符. public String name; protected String name; Str ...
- 无需debug,通过抽象模型快速梳理代码核心流程
上一篇我们通过DSM来确定了核心对象并构建了抽象模型.本篇是<如何高效阅读源码>专题的第八篇,我们来基于抽象模型来梳理核心流程. 本节主要内容: 如何通过抽象模型来梳理核心流程 从类名和注 ...