【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

标签: LeetCode


题目地址:https://leetcode.com/problems/evaluate-reverse-polish-notation/description/

题目描述:

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 有个牛逼的函数,就是eval(),可以给它一个运算表达式,直接给你求值。中缀表达式转正常表达式很简单了,直接用栈就行。

但是!!需要注意的是,python中的’/’负数除法和c语言不太一样。在python中,(-1)/2=-1,而在c语言中,(-1)/2=0。也就是c语言中,除法是向零取整,即舍弃小数点后的数。而在python中,是向下取整的。而这道题的oj是默认的c语言中的语法,所以需要在遇到’/’的时候注意一下。

一种方式是采用负负得正的方法,用两个负号变成整数的除法,再取负。

另一种方式是使用operator.truediv(int(a), int(b))变成和c相同的方式。

class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
operators = ['+', '-', '*', '/']
for token in tokens:
if token not in operators:
stack.append(token)
else:
b = stack.pop()
a = stack.pop()
if token == '/' and int(a) * int(b) < 0:
res = eval('-' + '(' + '-' + a + '/' + b + ')')
else:
res = eval(a + token + b)
stack.append(str(res))
return int(stack.pop())

或者:

class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
operators = ['+', '-', '*', '/']
for token in tokens:
if token not in operators:
stack.append(token)
else:
b = stack.pop()
a = stack.pop()
if token == '/':
res = int(operator.truediv(int(a), int(b)))
else:
res = eval(a + token + b)
stack.append(str(res))
return int(stack.pop())

日期

2018 年 3 月 14 日

【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)的更多相关文章

  1. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  2. Java for LeetCode 150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  4. leetcode 150. Evaluate Reverse Polish Notation ------ java

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  6. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

  7. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

  8. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  9. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

随机推荐

  1. HBase【操作Java api】

    一.导入依赖 创建模块,导入以下依赖,maven默认编译版本是1.5,用1.8编译. pom.xml <dependencies> <dependency> <group ...

  2. 爬虫系列:连接网站与解析 HTML

    这篇文章是爬虫系列第三期,讲解使用 Python 连接到网站,并使用 BeautifulSoup 解析 HTML 页面. 在 Python 中我们使用 requests 库来访问目标网站,使用 Bea ...

  3. myatoi

    atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中.int atoi(const char *nptr) 函数会扫描参数 nptr字符串 ...

  4. 【swift】长按事件绑定,平移滑动事件+坐标获取

    为何把这两个事件归类在一起? 我后来才明白,iOS有一个手势事件(UiGestureRecognizer) 事件里有7个功能,不过我只试过前两个,也就是标题的这两个(长按.平移滑动) UILongPr ...

  5. jenkins之分布式

    在jenkins的slave节点安装jdk(注:slave节点不需要安装jenkins) #:安装jdk环境 root@ubuntu:/usr/local/src# ls jdk-8u191-linu ...

  6. Linux学习 - 分区与文件系统

    一.分区类型 1 主分区:总共最多只能分四个 2 扩展分区:只能有一个(主分区中的一个分区),不能存储数据和格式化,必须再划分成逻辑分区                               才 ...

  7. 在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题

    在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题 此时解决办法是 #pragma mark - UIImagePickerController D ...

  8. zabbix之故障自治愈和分层报警

    在agent端修改配置文件 root@ubuntu:~# vim /etc/sudoers zabbix ALL=(ALL) NOPASSWD:ALL#:重启服务root@ubuntu:~# syst ...

  9. 如何在linux 上配置NTP 时间同步?

    故障现象: 有些应用场景,对时间同步的要求严格,需要用到NTP同步,如何在linux上配置NTP时间同步? 解决方案: 在linux 上配置NTP 时间同步,具休操作步骤,整理如下: 1.安装软件包( ...

  10. Docker从入门到精通(三)——概念与执行流程

    前面我们大概介绍了docker是什么以及如何安装docker,但是对里面出现的一些名词,可能大家还不熟悉,这篇文章就来为大家解惑. 1.容器化平台 Docker 是提供应用打包,部署与运行应用的容器化 ...