[leetcode]Evaluate Reverse Polish Notation @ Python
原题地址:https://oj.leetcode.com/problems/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
解题思路:这道题是经典的逆波兰式求值。具体思路是:开辟一个空栈,遇到数字压栈,遇到运算符弹出栈中的两个数进行运算,并将运算结果压栈,最后栈中只剩下一个数时,就是所求结果。这里需要注意的一点是python中的'/'除法和c语言不太一样。在python中,(-1)/2=-1,而在c语言中,(-1)/2=0。也就是c语言中,除法是向零取整,即舍弃小数点后的数。而在python中,是向下取整的。而这道题的oj是默认的c语言中的语法,所以需要在遇到'/'的时候注意一下。
代码:
class Solution:
# @param tokens, a list of string
# @return an integer
def evalRPN(self, tokens):
stack = []
for i in range(0,len(tokens)):
if tokens[i] != '+' and tokens[i] != '-' and tokens[i] != '*' and tokens[i] != '/':
stack.append(int(tokens[i]))
else:
a = stack.pop()
b = stack.pop()
if tokens[i] == '+':
stack.append(a+b)
if tokens[i] == '-':
stack.append(b-a)
if tokens[i] == '*':
stack.append(a*b)
if tokens[i] == '/':
if a*b < 0:
stack.append(-((-b)/a))
else:
stack.append(b/a)
return stack.pop()
[leetcode]Evaluate Reverse Polish Notation @ Python的更多相关文章
- LeetCode: Evaluate Reverse Polish Notation 解题报告
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode——Evaluate Reverse Polish Notation 求算式值(AC)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation stack 栈
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] Evaluate Reverse Polish Notation [2]
题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 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 ...
随机推荐
- commonjs,amd,cmd
在某些库中,经常会看到函数最前面有一个分号.其实是为了防止自动化工具拼接js时,如果前面的js文件的结尾处忘了加分号,拼接出来的代码容易挂,加分号这种行为属于防御式编程. 一个模块就是实现特定功能的文 ...
- 【BZOJ 3620】 3620: 似乎在梦中见过的样子 (KMP)
3620: 似乎在梦中见过的样子 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 755 Solved: 445 Description “Madok ...
- C++ 队列(queue)堆栈(stack)实现基础
Queue 在C++中只要#include<queue>即可使用队列类,其中在面试或笔试中常用的成员函数如下(按照最常用到不常用的顺序) 1. push 2. pop 3. size 4. ...
- 钻牛角尖还是走进死胡同--shell脚本根据名称获得 dubbo 服务的 pid
到了下午,突然觉得坐立不安,可能是因为中午没有休息好.老大不小了还在做页面整合的事情,这是参加工作时就干的工作了.然后突然想去挑战高级一点的缺陷排查,结果一不小心就钻了一个牛角尖.启动 dubbo 服 ...
- maven执行update命令时报org/apache/maven/shared/filtering/MavenFilteringException错误
原 maven执行update命令时报org/apache/maven/shared/filtering/MavenFilteringException错误 在eclipse中对准项目执行maven- ...
- C#、Java、Javascript获取Unix时间戳
背景: 因为项目需要,需要几种语言联动开发,日期字段设计的数字型 获取Unix时间戳代码: Java System.currentTimeMillis() Javascript new Date(). ...
- 负载均衡介绍及Nginx简单实现
负载均衡介绍及Nginx简单实现 负载均衡 负载均衡介绍及Nginx简单实现 1. 介绍 2. 常用的开源软件 2.1 LVS 优点 缺点 2.2 Nginx 优点 缺点 3. 常用的开源反向代理软件 ...
- git 用户名和密码保存
git config --global credential.helper store 输入一次后,后续不再需要输入用户名密码
- ubuntu下安装ftp服务器
参考文献: 5.4 FTP 服务器 vsftpd - FTP 服务器安装 vsftpd 是可在 Ubuntu 中使用的 FTP 守护程序之一.它在安装.设置和维护方面十分方便.要安装 vsftpd 您 ...
- 使用git pull文件时和本地文件冲突怎么办
在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息:error: Your local changes to 'c/environ.c' would be overwritten by ...