题目来源:

  https://leetcode.com/problems/evaluate-reverse-polish-notation/


题意分析:

  给定一个数组,用这个数组来表示加减乘除,例如

["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

题目思路:

  这里考虑的是栈的使用。如果是数字那么push进栈,如果不是,那么把栈后两个数pop出来,进行相应的操作。要注意的是除法的时候要保证先转化正负一致再进行计算。


代码(python):

class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
ans = []
for i in tokens:
if i != '/' and i != '*' and i != '+' and i != '-':
ans.append(int(i))
else:
tmp1 = ans.pop()
tmp2 = ans.pop()
if i == '/':
if tmp1*tmp2 < 0:
ans.append(-((-tmp2) // tmp1))
else:
ans.append(tmp2/tmp1)
if i == '*':
ans.append(tmp2*tmp1)
if i == '+':
ans.append(tmp2 + tmp1)
if i == '-':
ans.append(tmp2 - tmp1)
return ans[0]

[LeetCode]题解(python):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. 记一次phpStudy apache启动后自动关闭 修改过程

    第一种可能原因:路径包含中文 .添加站点 2.重启服务 3.遇见问题 apache 刚启动,1秒钟中后就停止 4.解决问题 发现是自己添加的网站中包含中文路径的问题,建议不要在自己的网站目录下包含中文 ...

  2. 安装wxWidgets

    从官网可下载源码安装包.安装后,建立批处理文件对其进行编译: gvim bwx.bat 内容如下: @echo off set WXWIN=C:\wxWidgets-3.0.2 pushd %WXWI ...

  3. C语言静态函数静态变量

    C语言程序可以看成由一系列外部对象构成,这些外部对象可能是变量或函数.而内部对象是指定义在函数内部的函数参数及变量. 外部变量定义在函数之外,因此可以在许多函数中使用.由于C语言不允许在一个函数中定义 ...

  4. 用SQL将查询出来的多列的值拼接成一个字符串【转载】

    MySQL中: [sql] view plaincopyprint? -- 单列拼接,先查出一行,再加上逗号,接着拼接 查出的下一行 select group_concat(E.SUPPORT) fr ...

  5. jsp中包含JAVA代码

    在JSP中大部分都是由脚本小程序组成,所谓的脚本小程序就是里面直接包含了JAVA代码. 在JSP中Scriptlet一共分为三种:        · <%%>:定义局部变量,编写语句    ...

  6. 表格(table) 插件:支持当前行增行、删除。使用事件委托

    最近做一个项目,需要对表格进行增行和删行. 研究了一下jquery操作dom的方法和事件委托原理,下面是我编写的例子,源码传上,欢迎高手指点. 功能: 支持在指定行下面增行: 支持删行指定行: 增行. ...

  7. 【译】Optimize for mobile-移动端优化

    移动端优化 由于移动设备有限的CPU处理能力,移动网络的高往返时间,以及移动应用的快速增长,对移动端的理解以及优化相对于PC端来讲变得更加重要.Page Speed Insights 现在可以让你轻松 ...

  8. model、dao、 service 和Comtroll层的关系

    首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操 ...

  9. IBM developerWorks 的Ajax系列教程

    掌握 Ajax,第 1 部分: Ajax 入门简介 http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro1.html?csrf-799150205 ...

  10. 初学swift笔记 函数(六)

    import Foundation /* func 函数名 (参数名:参数类型) { } func 函数名 (参数名:参数类型) ->Void{ } func 函数名 (参数名:参数类型) -& ...