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. js 学习二 字符串常用方法

    1.字符串长度 string.length var browserType = 'mozilla'; browserType.length; //7 2在字符串中查找子字符串 string.index ...

  2. c# task笔记

    class Program { //创建Task任务的三种方式(无返回值) static void Main(string[] args) { //方法一 Task task1 = new Task( ...

  3. 2019-11-29-dotnet-判断特定进程存在方法

    title author date CreateTime categories dotnet 判断特定进程存在方法 lindexi 2019-11-29 08:34:18 +0800 2019-09- ...

  4. IBM DS5020 管理口密码重置

    IBM DS5020 管理口密码重置 使用超级终端进行连接,输入回车,然后Ctrl+Break(有时需要多按几次),屏幕会出现设置波特率的提示: Send for shell access or ba ...

  5. 关于PHP://input

    $data = file_get_contents("php://input");    php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好 ...

  6. [NoSql注入] MongoDB学习

    0x00 安装 下载:http://dl.mongodb.org/dl/win32/x86_64 安装:http://www.runoob.com/mongodb/mongodb-window-ins ...

  7. python打开文件的方式

    r 以只读模式打开文件 w   以只写模式打开文件,文件若存在,首先要清空,然后(重新创建) a    以追加模式打开(从EOF开始,必要时创建新文件),把所有要写入文件的数据追加到文件的末尾,即使使 ...

  8. 记一次使用dubbo过程中版本冲突导致的坑

    前言:2019年09月23日临下班,产品的一个变更需求临近尾声阶段.本地测试OK,兴致冲冲的想着发布到测试环境,验证一下没有问题,五分钟结束战斗,明天就开始下个需求了.随在CE(公司的devOps系统 ...

  9. 2019hdu多校 Minimal Power of Prime

    题目链接:Click here 题目大意:求一个数分解质因数后的最小幂指数 Solution: 首先,我们肯定是不能直接暴力求解的 我们先考虑筛出1e4范围以内的所有质数,把x所有这个范围内的质因子筛 ...

  10. Java 内置锁 重入问题

    阅读<Java并发编程实战>,遇到了一个问题 代码如下 /** * @auther draymonder */ public class Widget { public synchroni ...