【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 +, -, ...
随机推荐
- linux之sed用法
参考 http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行 ...
- IntelliJ IDEA For Mac 快捷键
Mac键盘符号和修饰键说明 ⌘ Command ⇧ Shift ⌥ Option ⌃ Control ↩︎ Return/Enter ⌫ Delete ⌦ 向前删除键(Fn+Delete) ↑ 上箭头 ...
- oracle---日期等plsql
日期/时间 相关查询 1.获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 "SYSDATE"来指定查询的日期. SELECT TRUNC ( ...
- css3 动画效果 定义和绑定执行
首先要定义一个动画效果 keyframes 关键字 这里动画效果执行完毕后 恢复本身的css样式 有的动画效果 移动到位置 要保持 就需要写好css 元素的位置 css里直接写 (这里是一般的 ...
- 如何设置phpMyAdmin自动登录和取消自动登录
如何设置phpMyAdmin自动登录? 首先在根目录找到config.sample.inc.php复制一份文件名改为config.inc.php(如果已经存在 config.inc.php 文件,则直 ...
- Tortoise SVN 版本控制常用操作汇总(show log)
1.如何查看SVN上当前代码库的最新版本号是多少? 打开右键菜单中的 show log,然后看到一系列版本更新历史,最上面的那一行,即是最新版本号,所谓的 head revision. 2.如何查看本 ...
- C/C++ 静态链接库(.a) 与 动态链接库(.so)
平时我们写程序都必须 include 很多头文件,因为可以避免重复造轮子,软件大厦可不是单靠一个人就能完成的.但是你是否知道引用的那些头文件中的函数是怎么被执行的呢?这就要牵扯到链接库了! 库有两种, ...
- Asp.Net Core--简单的授权
翻译如下: 在MVC中授权通过控制AuthorizeAttribute属性及其各种参数.在最简单的应用AuthorizeAttribute属性控制器或行动限制访问控制器或操作任何身份验证的用户. 例如 ...
- HTML5CSS3特效-上下跳动的小球-遁地龙卷风
(-1)写在前面 我用的是chrome49,这个idea是我在stackoverflow上回答问题时看到了,多谢这位同行,加深了我对很多技术点的理解,最近刚到北京,忙碌了一两天,在后续的日子里,会被安 ...
- 网址前面的icon
shortcut icon和icon代码之间究竟有何区别呢.下面介绍一下 语句一:<link rel="shortcut icon" href="favicon ...