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 ...
随机推荐
- MySQL大事务导致的Insert慢的案例分析
[问题] 有台MySQL服务器不定时的会出现并发线程的告警,从记录信息来看,有大量insert的慢查询,执行几十秒,等待flushing log,状态query end [初步分析] 从等待资源来看, ...
- 1036 Boys vs Girls (25)(25 point(s))
problem This time you are asked to tell the difference between the lowest grade of all the male stud ...
- CVE-2014-4113本地提权测试
CVE-2014-4113本地提权漏洞分析 By Netfairy 前言 2014年10月14日, Crowdstrike和FireEye发表了一篇文章, 描述了一个新的针对Windows的提权漏洞. ...
- setTimeout 第一个参数类型
读别人代码的时候看到这么一段,很不理解,然后就搜了一下百度 setTimeout / setInterval 第一个参数可以有三种类型: 字符串 . methods . 匿名函数 1.字符串 ...
- Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分
D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
- linux—文件目录简单介绍
1.Linux系统以文件目录的方式来组织和管理系统中的所有文件.所谓文件目录就是将所有文件的说明信息采用树型结构组织起来,即我们常说的目录:整个文件系统有一个“根”(root),然后在根上分“杈”(d ...
- ROS知识(10)----smach_viewer的Graph view不能显示状态图
1.问题 在运行ROS by Example 2--Indigo版本中,运行 smach_viewer,再运行巡逻,命令如下: $ rosrun smach_viewer smach_viewer.p ...
- Hibernate与MyBatis的对比总结
最近做了一个Hibernate与MyBatis的对比总结,希望大家指出不对之处. 第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出 ...
- Android Tasker应用之自动查询并显示话费流量套餐信息
Android Tasker应用之自动查询并显示话费流量套餐信息 虽然Android平台有非常多的流量监控软件,但最准确的流量数据还是掌握在运营商手里.有些朋友可能像我一样时不时地发短信查询流量信息, ...