Evaluate Reverse Polish Notation leetcode java
题目:
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
题解:
这道题,其实上过课的应该都知道,忘记是哪门课讲得了。。知道原理的话就比较容易写出来。下面引用一下什么是Reverse Polish Notation:
“标准的表达式如"A+B",在数学上学名
叫中缀表达式(Infix Notation),原因是运算符号在两个运算对象的中间。相对应的还有前缀表达式(Prefix
Notation),如:"+ - A * B C D",转换成中缀表达式为:"A - B * C + D";后缀表达式(Postfix
Notation),比如前所述的中缀表达式转换为后缀表达式为:"A B C * - D +"。为了纪念波兰数学家鲁卡谢维奇(Jan
Lukasiewicz),前缀表达式被称作波兰表达式,后缀表达式称为逆波兰表达式(Reverse Polish Notation)。
后缀表达式的优点是显而易见的,编译器在处理时候按照从左至右的顺序读取逆波兰表达式,遇到运算对象直接压入堆栈,遇到运算符就从堆栈提取后进的两个对象进行计算,这个过程正好符合了计算机计算的原理。
后缀表达式比前缀表达式更加易于转换,并且它的最左面一定为数字,这一点在实际编程的时候就会体会到它的好处了。
逆波兰表达式有一个更大的优点,就是拆括号,根据运算符的级别将中缀表达式转换成逆波兰表达式后,运算顺序就已经替代了运算符的级别,这样也避免了括号提高运算级别的特殊处理。
事
实上,人的思维方式很容易固定~~!正如习惯拉10进制
1 public static int evalRPN(String[] tokens) {
2 if(tokens==null||tokens.length==0)
3 return 0;
4 int ans = 0;
5 Stack<Integer> res = new Stack<Integer>();
6 for(int i = 0; i<tokens.length;i++){
7 ans = 0;
8 if(tokens[i].equals("/")||tokens[i].equals("*")||tokens[i].equals("+")||tokens[i].equals("-")){
9 int b = res.pop();
int a = res.pop();
if(tokens[i].equals("/"))
ans += a/b;
else if(tokens[i].equals("+"))
ans += a+b;
else if(tokens[i].equals("-"))
ans += a-b;
else if(tokens[i].equals("*"))
ans += a*b;
res.push(ans);
}else{
res.push(Integer.parseInt(tokens[i]));
}
}
return res.pop();
}
。就对2,3,4,8,16等进制不知所措一样~~!人们习惯的运算方式是中缀表达式。而碰到前
缀,后缀方式。。迷茫其实仅仅是一种表达式子的方式而已(不被你习惯的方式)我这里教你一种也许你老师都没跟你讲的简单转换方式一个中缀式到其他式子的转
换方法~~这里我给出一个中缀表达式~a+b*c-(d+e)
第一步:按照运算符的优先级对所有的运算单位加括号~
式子变成拉:((a+(b*c))-(d+e))
第二步:转换中缀与后缀表达式
后缀:把运算符号移动到对应的括号后面
则变成拉:((a(bc)*)+(de)+)-
把括号去掉:abc*+de+- 后缀式子出现
发现没有,前缀式,后缀式是不需要用括号来进行优先级的确定的。
现在,你需要用计算机来实现这一过程,怎么样,是否有兴趣一试呢?如果答案是肯定的话,Let‘s go!”
以上内容引用自:http://puppypuppy2005.blog.163.com/blog/static/5204815620107523258709/ 代码如下:
Evaluate Reverse Polish Notation leetcode java的更多相关文章
- 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
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 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. 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: 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 解题报告(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 ...
随机推荐
- Python 学习经历分享
如果说 Java 是亲儿子的话,那么 Python 应该就是干儿子了.看了一下所有关于 Python 的笔记,我发现原来我在 4 月份的时候就已经涉足 Python 了,但是到目前为止才真正算做出了一 ...
- hihocoder 1509 异或排序
题面在这里! 考虑前后两个数 x,y,可以发现S只有在(x xor y)的最高有1位上的取值是要被确定的 (如果x==y那么没有限制),可以推一下什么情况下是1/0. 于是我们模拟一下这个操作,判一判 ...
- windows提权exp列表
漏洞列表 #Security Bulletin #KB #Description #Operating System CVE-2017-0213 [Windows COM Elevation of P ...
- Curl 及 Curl的使用介绍
Curl 简介 Curl是Linux下一个很强大的http命令行工具,其功能十分强大. 1) 二话不说,先从这里开始吧! $ curl http://www.linuxidc.com 回车之后,www ...
- 【洛谷】3469:[POI2008]BLO-Blockade【割点统计size】
P3469 [POI2008]BLO-Blockade 题意翻译 在Byteotia有n个城镇. 一些城镇之间由无向边连接. 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些).每 ...
- 计算机二级软件VC++6.0下载地址
计算机二级软件VC++6.0介绍: 适合所有参加全国计算机等级考试的童鞋们……见图如下: 下载地址:(以下两者任选其一即可) (1).计算机二级软件VC++6.0(16.35MB) (2).计算机二级 ...
- C#设计模式泛型注入
TSFac注入方式: 泛型接口工厂: public class SFac<TInterface, TClass> where TInterface : class where TClass ...
- List常用子类的特点
ArrayList: 底层数据结构是数组,查询快,增删慢 线程不安全, 效率较高 Vector 底层数据结构是数组,查询快,增删慢 线程安全, 效率较低 LinkedList 底 ...
- 使用HAproxy如何实现web站点的动静分离
HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy特别 适用于那些负载特大的web站点,这些站点通常 ...
- 关于myBatis的问题There is no getter for property named 'USER_NAME' in 'class com.bky.model.实例类'
现在流行的 ssm(spring + struts2 + myBatis) 持久层的mybatis是需要配置映射器的,找了个demo连接的数据库是MySQL 于是就修改了一下弄成了连接Oracle的 ...