【leetcode】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
*简单的来说就是计算逆波兰式的值。
解题思路:
这就是一个弹栈进栈的问题,根据弹出的元素类型和栈顶的元素的类型不同得出不同的结果。输入里卖弄无非就是数字和运算符这两种情况,组合一下有下面这几种情况:
1.弹出的是数字,下一个还是数字:压栈,等待运算符进行计算
2.弹出的是数字,下一个是运算符:压栈,等待运算符计算
3.弹出的是运算符,下一个是数字:从栈中弹出两个操作数计算
4.弹出的是运算符,下一个是运算符:从栈中取出两个操作数计算
综合一下就是:数字就压栈,运算符就弹出两个数计算
需要注意的:
注意这里除法的取整方式,应该是先计算出浮点数的结果再取整int(op[-2]*1.0 / op[-1]*1.0)
class Solution:
# @param tokens, a list of string
# @return an integer
def evalRPN(self, tokens):
ops = ['+','-','*','/']
l = len(tokens)
op = []
for i in range(l):
if tokens[i] == '+':
t = op[-1] + op[-2]
op.pop()
op.pop()
op.append(t)
elif tokens[i] == '-':
t = op[-2] - op[-1]
op.pop()
op.pop()
op.append(t)
elif tokens[i] == '*':
t = op[-1] * op[-2]
op.pop()
op.pop()
op.append(t)
elif tokens[i] == '/':
t = int(op[-2]*1.0 / op[-1]*1.0)
op.pop()
op.pop()
op.append(t)
else:
op.append(int(tokens[i]))
return op[-1] def main():
s = Solution()
print s.evalRPN(["","","","","+","-11","*","/","*","","+","","+"]) if __name__ == '__main__':
main()
【leetcode】Evaluate Reverse Polish Notation的更多相关文章
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【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 ...
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
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 +, -, ...
- 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 +, -, ...
随机推荐
- Thinking in java学习笔记之map的应用
Random rand = new Random(47); Map<Integer,Integer> m = new HashMap<Integer,Integer>(); f ...
- AOJ DSL_2_C Range Search (kD Tree)
Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...
- <<< commons-fileupload 和 ajaxfileupload 实现局部上传
最近弄了一个上传,要求实现页面的局部刷新,Java的上传组件大多还是用的 commons-fileupload,网上搜索了好多的教程,太麻烦了,看到了ajaxfileupload这个插件,不错,实现简 ...
- BZOJ4650: [Noi2016]优秀的拆分
考场上没秒的话多拿5分并不划算的样子. 思想其实很简单嘛. 要统计答案,求以每个位置开始和结束的AA串数量就好了.那么枚举AA中A的长度L,每L个字符设一个关键点,这样AA一定经过相邻的两个关键点.计 ...
- 【转】精选30个优秀的CSS技术和实例
今天,我为大家收集精选了30个使用纯CSS完成的强大实践的优秀CSS技术和实例,您将在这里发现很多与众不同的技术,比如:图片集.阴影效果.可扩展按钮.菜单等-这些实例都是使用纯CSS和HTML实现的. ...
- Google map markers
现已被屏蔽 http://mabp.kiev.ua/2010/01/12/google-map-markers/ Надо по немногу отходить от празднывания но ...
- CentOS个人目录下中文路径转英文路径
CentOS个人目录下中文路径转英文路径 如果安装了中文版到CentOS之后,root目录及home目录下会出现中文到路径名,如"桌面"."文档"," ...
- JDK Collection 源码分析(2)—— List
JDK List源码分析 List接口定义了有序集合(序列).在Collection的基础上,增加了可以通过下标索引访问,以及线性查找等功能. 整体类结构 1.AbstractList 该类作为L ...
- IHttpModule生命周期
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- C#学习笔记
1.C#中[],List,Array,ArrayList的区别 [] 是针对特定类型.固定长度的. List 是针对特定类型.任意长度的. Array 是针对任意类型.固定长度的. ArrayList ...