原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/

题意:

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

代码:

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]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. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  3. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  4. Leetcode Evaluate Reverse Polish Notation

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

  5. leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

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

  6. [LeetCode] Evaluate Reverse Polish Notation stack 栈

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

  7. [LeetCode] Evaluate Reverse Polish Notation [2]

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

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

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

  9. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

随机推荐

  1. jquery 下拉列表选择值

    选择下拉列表值样例方式 $("#updateAppName").get(0).selectedIndex = 0; $("#updateAppName").ge ...

  2. [ZOJ3781]Paint the Grid Reloaded

    思路: 先用DFS缩点,然后BFS找出每个点出发能到达的最长路,取$min$. 注意多组数据,初始化一定要仔细,刚开始存边的$e$忘记初始化,一直WA,调了半个晚上. 一开始和网上的题解对拍$T=1$ ...

  3. 【BZOJ-4180】字符串计数 后缀自动机 + 矩阵乘法

    4180: 字符串计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 146  Solved: 66[Submit][Status][Discuss] ...

  4. 【BZOJ-4261】建设游乐场 最大费用最大流

    4261: 建设游乐场 Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 21  Solved: 8[Submit][Status][Discuss] D ...

  5. 面试必会函数源代码 strcpy/memcpy/atoi/kmp/quicksort

    http://blog.csdn.net/liuqiyao_01/article/details/26967813 二.stl模板函数 1.strcpy char * strcpy( char *st ...

  6. mysql导入csv文件

    今天尝试将Oracle中的数据导入到mysql中,在SQLyog工具其中看到一些sql语句,拿来记录一下,说不定以后就用的着呐! -----查看ydtf数据库中的基础表,就是用户创建了哪些表 SHOW ...

  7. Linux下以.rc结尾的文件含义

    运行命令 资源控制 运行控制 运行时配置 其实我更青睐于运行时配置,也就是运行时的变量等放置变量的. 参考: https://stackoverflow.com/questions/11030552/ ...

  8. MikroTik RouterOS网址资源收集

    routeros|mikrotik|ros|软路由论坛|中国路由网|软件路由|软件路由器|routeros技术论坛|路由论坛 - Powered by Discuz!   Mikrotik RB450 ...

  9. Mac 10.12彻底关闭Dashboard

    1.打开[系统偏好设置]. 2.点击进入[Mission Control]. 3.中间有一项[Dashboard],点击它右边的下拉菜单,选择[关闭],即可.

  10. HDU 4741 Save Labman No.004 (2013杭州网络赛1004题,求三维空间异面直线的距离及最近点)

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...