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
*简单的来说就是计算逆波兰式的值。

解题思路:


这就是一个弹栈进栈的问题,根据弹出的元素类型和栈顶的元素的类型不同得出不同的结果。输入里卖弄无非就是数字和运算符这两种情况,组合一下有下面这几种情况:
1.弹出的是数字,下一个还是数字:压栈,等待运算符进行计算
2.弹出的是数字,下一个是运算符:压栈,等待运算符计算
3.弹出的是运算符,下一个是数字:从栈中弹出两个操作数计算
4.弹出的是运算符,下一个是运算符:从栈中取出两个操作数计算
综合一下就是:数字就压栈,运算符就弹出两个数计算

需要注意的:

注意这里除法的取整方式,应该是先计算出浮点数的结果再取整int(op[-2]*1.0 / op[-1]*1.0)

 class Solution:
# @param tokens, a list of string
# @return an integer
def evalRPN(self, tokens):
ops = ['+','-','*','/']
l = len(tokens)
op = []
for i in range(l):
if tokens[i] == '+':
t = op[-1] + op[-2]
op.pop()
op.pop()
op.append(t)
elif tokens[i] == '-':
t = op[-2] - op[-1]
op.pop()
op.pop()
op.append(t)
elif tokens[i] == '*':
t = op[-1] * op[-2]
op.pop()
op.pop()
op.append(t)
elif tokens[i] == '/':
t = int(op[-2]*1.0 / op[-1]*1.0)
op.pop()
op.pop()
op.append(t)
else:
op.append(int(tokens[i]))
return op[-1] def main():
s = Solution()
print s.evalRPN(["","","","","+","-11","*","/","*","","+","","+"]) if __name__ == '__main__':
main()

【leetcode】Evaluate Reverse Polish Notation的更多相关文章

  1. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  2. 【leetcode】Evaluate Reverse Polish Notation(middle)

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

  3. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  4. leetcode - [2]Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...

  5. 【leetcode刷题笔记】Evaluate Reverse Polish Notation

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

  6. [LeetCode] 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 +, -, ...

随机推荐

  1. jquery 实现类似于弹幕效果

    在别人网站中看到一个类似于弹幕的效果,闲来无事用jquery写了个备用~~ <!DOCTYPE html> <meta charset="utf-8"> & ...

  2. jenkins持续集成源码管理选项为None,构建失败找不到git.exe解决办法

    我的jenkins版本为Jenkins ver. 2.19.1 1.源码管理选项只有None的解决办法: 在插件管理中心,搜索对应的源码管理插件这里以git为例,搜索git plugin点击右下角的安 ...

  3. IBatis存储过程返回值

    <parameterMaps> <parameterMap id="delVersionBagInfoParam" class="DelVersionB ...

  4. 【原创】Weblogic 反序列化远程命令执行漏洞GetShell&Cmd Exploit

    这工具写到半夜四点,做个记录. 已发布至freebuf,链接:http://www.freebuf.com/vuls/90802.html

  5. git操作---查询

    1.查看git的状态   git status 2.查看git的日志历史记录 git log 3.查看当前git的分支 git branch 4.查看git的配置信息 git config --lis ...

  6. C++ ## ... 实用

    关于...的使用...在C宏中称为Variadic Macro,也就是变参宏.比如:#define myprintf(templt,...)fprintf(stderr,templt,__VA_ARG ...

  7. Bash 中的环境变量

    在 Bash 里,可以通过 export 命令查看当前 Shell 进程的环境变量,这些环境变量一些是 Bash 自己创建的,还有一些是 Bash 从父进程继承来的,然而需要注意的是,父进程传给 Ba ...

  8. ASP.NET学习链接

    张子阳个人ASP.NET技术博客:http://www.tracefact.net/Asp-Net/ 动态加载asp.net分页控件:http://www.cnblogs.com/cresuccess ...

  9. [Network] 计算机网络基础知识总结

    计算机网络学习的核心内容就是网络协议的学习.网络协议是为计算机网络中进行数据交换而建立的规则.标准或者说是约定的集合.因为不同用户的数据终端可能采取的字符集是不同的,两者需要进行通信,必须要在一定的标 ...

  10. PHP如何将session保存到memcached中?如何分布式保存PHP session

    session_set_save_handler无关的memcached保存session的方法 在memcached服务器上 1)下载memcached #wget http://memcached ...