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. css3弹性伸缩布局(一)—————flex布局

    CSS3弹性伸缩布局简介 2009年,W3C提出了一种崭新的方案—-Flex布局(即弹性伸缩布局),它可以简便.完整.响应式地实现各种页面布局,包括一直让人很头疼的垂直水平居中也变得很简单地就迎刃而解 ...

  2. ubuntu中apache的ssl证书配置及url重写

    一.https原理 借用网上的图(图片来源: https://www.cnblogs.com/xiohao/p/9054355.html ),用到了对称加密和非对称加密.    二.ubuntu的ap ...

  3. intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件

    1. 错误信息 Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException ...

  4. Matlab 中 Data-driven 风格的 API 设计

    设计 所谓 data-driven API,指的是用户可以把"操作"作为参数,传入函数,像下面这种: stream = dataStream('load', 'example.cs ...

  5. k8s第一个脚本:hello world

    1.hello-world-pod.yaml 脚本: # cat hello-world-pod.yaml apiVersion: v1 kind: Pod metadata: name: hello ...

  6. Listview.Finditem()函数用法

    查找LISTVIEW控件中指定的字符串.   Private Sub ListView2_Click() On Error GoTo ONERROR Dim strFindMe As String   ...

  7. Heshen's Account Book HihoCoder - 1871 2018北京区域赛B题(字符串处理)

    Heshen was an official of the Qing dynasty. He made a fortune which could be comparable to a whole c ...

  8. P5018 对称二叉树题解

    题目内容链接: 那么根据题意,上图不是对称二叉树,只有节点7的子树是: 通俗来说,对称二叉树就是已一个节点x为根的子树有穿过x点的对称轴并且对称轴两边的对称点的大小也必须相等,那么这棵树就是对称二叉树 ...

  9. vue笔记(更新中)

    1.禁止div点击 2.禁止选择 -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; - ...

  10. linux就该这么学.pdf

    链接:https://pan.baidu.com/s/1mhYIqgg 密码:ay0j