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. Flask运行时指定端口

    在项目入口文件server.php中,有如下代码 if __name__ == '__main__': app.run(debug=True,port=8000) 但是在进入虚拟机中运行 flask ...

  2. 解决Response.AddHeader中文乱码问题

    string filename = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes("培训班自然情况表")); Response.AddH ...

  3. J2EE WEB应用架构分析

    1. 架构概述 J2EE体系包括java server pages(JSP) ,java SERVLET, enterprise bean,WEB service等技术.这些技术的出现给电子商务时代的 ...

  4. Jdbc Driver驱动和ServerTimeZone时区的的问题

    一.JDBC驱动的版本号以及名称问题 区别: com.mysql.jdbc.Driver 是 mysql-connector-java 5中的 com.mysql.cj.jdbc.Driver 是 m ...

  5. 处理webp加所有的jpg到指定路径

    #!/bin/shfunction getdir(){compareName='.webp';for element in `ls $1`dodir_or_file=$1"/"$e ...

  6. Docker下载镜像出现failed to register layer: symlink....问题

    在用Docker下载RabbitMQ的时候出现如下问题 个人解决方案:重启Docker. 若重启还是无法解决问题,可以先关闭Docker systemctl stop docker 然后把已下载的相关 ...

  7. poj 1664 放苹果(dfs)

    放苹果 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30284   Accepted: 19098 Description ...

  8. php 各种扩展 - 都有

    https://windows.php.net/downloads/pecl/releases/

  9. Rootkit XSS

    0x00 XSS Rootkit介绍 Rootkit概念: 一种特殊的恶意软件            类型: 常见为木马.后门等            特点: 隐蔽 持久控制 谈到XSS,一般都是想到 ...

  10. Java学习01-使用maven插件tomcat搭建web maven项目

    我使用社区版的IDEA,社区版的没有tomcat插件,只能使用maven插件进行tomcat的使用了,下面进入正题 一.pom.xml配置tomcat插件 <build> <fina ...