Evaluate Reverse Polish Notation——LeetCode
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
题目大意:给定一个String数组,数据是一个后缀表达式,求这个表达式的值。
解题思路:用栈来保存操作数即可,遇到数就入栈,遇到操作符就从栈里取出两个元素运算然后入栈即可,最后栈里只剩一个元素就是结果。这个不带括号还容易一点,直接上代码。
static Map<String, Integer> op = new HashMap<>(); static {
op.put("+", 1);
op.put("-", 2);
op.put("*", 3);
op.put("/", 4);
} public int evalRPN(String[] tokens) {
if (tokens == null || tokens.length == 0) {
return 0;
}
Stack<Integer> nums = new Stack<>();
for (String s : tokens) {
if (op.get(s) != null) {
int fa = nums.pop();
int fb = nums.pop();
if (op.get(s) == 1) {
nums.push(fb + fa);
} else if (op.get(s) == 2) {
nums.push(fb - fa);
} else if (op.get(s) == 3) {
nums.push(fb * fa);
} else {
nums.push(fb / fa);
}
} else {
nums.push(Integer.valueOf(s));
}
}
return nums.peek();
}
Evaluate Reverse Polish Notation——LeetCode的更多相关文章
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- Evaluate Reverse Polish Notation leetcode java
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...
- Evaluate Reverse Polish Notation --leetcode
原题链接:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目大意:给出逆波兰式,然后求其结果. 解题方法:单个栈 ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- 【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
随机推荐
- Nginx和Apache共存环境下apache获得真实IP
自从Nginx出现以后,我们都喜欢让 Nginx 跑在前方处理静态文件,然后通过 proxy 把动态请求过滤给 apache.这么有个问题,跑在后方 apache 上的应用获取到的IP都是Nginx所 ...
- 进程识别号(PID)的理解
PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...
- 解决ld: warning: directory not found for option警告
去掉警告的办法如下: 1选择工程, 编译的 (targets) 2选择 Build Settings 菜单 3查找 Library Search Paths 和 Framework Search Pa ...
- npm的镜像替换成淘宝
1.得到原本的镜像地址 npm get registry > https://registry.npmjs.org/ 设成淘宝的 npm config set registry http://r ...
- 《Python 二三事》——python学习必看(转载)
面向初学者介绍Python相关的一些工具,以及可能遇到的常见问题. 原文出处 原文作者:八八年出生的男性,互联网上常用id是 jagttt .目前正从事 IT 行业的工作.业余爱好是动漫游加电 ...
- windows7 jdk 环境变量添加
JAVA_HOME D:\Java;PATH %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;CLASSPATH .;%JAVA_HOME%\lib;%JAVA_HOME%\l ...
- Linux(Debian)下Maven的安装
Maven的下载地址:http://maven.apache.org/download.cgi这里以最新的3.3.9版本为例进行安装,在这之前需要确保机器上已经安装了JDK. -- 在home文件夹中 ...
- php基础知识【函数】(9)数学和对象类函数
一.数学 abs -- 绝对值 ceil -- 进一法取整 floor -- 舍去法取整 fmod -- 返回除法的浮点数余数 round -- 对浮点数进行四舍五入 sqrt -- 平方根 pi( ...
- Android Context作用
Context 用于访问全局信息的接口 App的资源: strings, drawable资源等等 工程代码:LearnContext.zip ---------------------------- ...
- Flask 富文本编辑器
XHEditor http://segmentfault.com/blog/digwtx/1190000002439076 CKeditor http://segmentfault.com/blog/ ...