mycode   42.30%、

注意:如果不考虑符号,-1//3=-1而不是等于0,因为是向下取整

class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
from collections import deque
def cal(data_1,data_2,item):
if item == '/':
return abs(data_2) // abs(data_1) *(-1 if (data_2 > 0) ^ (data_1 > 0) else 1)
elif item == '+':
return data_2 + data_1
elif item == '-':
return data_2 - data_1
else:
return data_2 * data_1
dq = deque()
calset = ['/','+','-','*']
for item in tokens:
if item in calset:
#print('if')
data_1 = dq.pop()
data_2 = dq.pop()
data = cal(int(data_1),int(data_2),item)
dq.append(data)
else:
#print('else')
dq.append(item)
#print(dq)
return dq[0]

参考:

#https://www.cnblogs.com/zuoyuan/p/3760530.html  注意负数的除法,c++和pytho的区别
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-mid-others-150. Evaluate Reverse Polish Notation的更多相关文章

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

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 150. Evaluate Reverse Polish Notation - LeetCode

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

  3. 【LeetCode】150. Evaluate Reverse Polish Notation

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

  4. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

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

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

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

  6. LeetCode OJ 150. Evaluate Reverse Polish Notation

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

  7. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

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

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

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

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

  10. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

随机推荐

  1. tornado源码简单实现

    首先基本的同步流程是: class Index(tornado.web.RequestHandle): def get(self): self.write('hellow,word') app = t ...

  2. 关于JAVA中的synchronized,一段不错的解释...

  3. Css布局 响应式布局介绍

    1. 概念: 写一套css样式可以同时适配多个终端,是为解决移动互联网诞生的. 2. 作用: 面对不同的分辨率设备灵活性强,能够快捷解决多设备显示适应问题 3. 原理 媒体查询 ① 外联式媒体查询语法 ...

  4. 上载和下载CSV文件

    sap中把txt .excel .文件上载到内表中,txt和csv速度最快. excel文件导出的csv是用,分隔符分隔的,如果单元格的文本中就有逗号,这样会和分隔符逗号混淆,最好abap产生csv文 ...

  5. 2019-11-29-Roslyn-如何在-Target-引用-xaml-防止文件没有编译

    title author date CreateTime categories Roslyn 如何在 Target 引用 xaml 防止文件没有编译 lindexi 2019-11-29 08:58: ...

  6. FMCJ450-基于ADRV9009的射频收发模块

    FMCJ450-基于ADRV9009的射频收发模块 一.板卡概述 接收路径包括两个具有动态范围的独立式宽带宽直接转换接收器.该器件还支持宽带宽分时观察路径接收器,供在 TDD 应用中使用.完整的接收子 ...

  7. laraveladmin省市区三级联动

    Distpicker是一个中国省市区三级联动选择组件,这个包是基于Distpicker的laravel-admin扩展,用来将Distpicker集成进laravel-admin的表单中 安装 com ...

  8. AIX中的进程管理

    1.AIX中的进程 (1)后台进程 后台进程运行时,用户不必等待当前后台进程的结束,即可以运行下一个进程. 后台进程的运行方式: # command &   (2)提高进程优先等级 -- ni ...

  9. 生成不带版本的jar包 不影响deploy

    1 How to build maven project without version? 工程pom中增加 <project> ... <build> ... <fin ...

  10. VMware导入ova报错

    报错如下: 此主机支持Intel VT-x,但Intel VT-x处于禁用状态.   解决方案如下: 联想E75主机,重启按F1进入BIOS Advanced—>CPU setup—>In ...