博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode150 Evaluate Reverse Polish Notation
阅读量:4221 次
发布时间:2019-05-26

本文共 1044 字,大约阅读时间需要 3 分钟。

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
Subscribe to see which companies asked this question

python

class Solution(object):    def isOp(self,s):        return s=="+" or s=="-" or s=="*" or s=="/"    def add(self,op1,op2):        return op1+op2    def sub(self,op1,op2):        return op1-op2    def mul(self,op1,op2):        return op1 * op2    def div(self,op1,op2):        if op1*op2<0:            return -((-op1)/op2)        return op1/op2    operator = {
"+":add,"-":sub,"*":mul,"/":div} def evalRPN(self,tokens): nums=[] for s in tokens: if self.isOp(s): op2 = nums.pop() op1 = nums.pop() re = self.operator.get(s)(self,op1,op2) nums.append(re) else: nums.append(int(s)) return nums[0]

转载地址:http://uwqmi.baihongyu.com/

你可能感兴趣的文章
游戏感:虚拟感觉的游戏设计师指南——第十八章 我想做的游戏
查看>>
游戏设计的艺术:一本透镜的书——第十章 某些元素是游戏机制
查看>>
游戏设计的艺术:一本透镜的书——第十一章 游戏机制必须平衡
查看>>
游戏设计的艺术:一本透镜的书——第十二章 游戏机制支撑谜题
查看>>
游戏设计的艺术:一本透镜的书——第十三章 玩家通过界面玩游戏
查看>>
编写苹果游戏中心应用程序(翻译 1.3 为iOS应用程序设置游戏中心)
查看>>
编写苹果游戏中心应用程序(翻译 1.4 添加游戏工具包框架)
查看>>
编写苹果游戏中心应用程序(翻译 1.5 在游戏中心验证本地玩家)
查看>>
编写苹果游戏中心应用程序(翻译 1.6 获取本地玩家的信息)
查看>>
编写苹果游戏中心应用程序(翻译 1.7 在游戏中心添加朋友)
查看>>
编写苹果游戏中心应用程序(翻译 1.8 获取本地玩家的好友信息)
查看>>
WebGL自学教程《OpenGL ES 2.0编程指南》翻译——勘误表
查看>>
WebGL自学教程——WebGL示例:12. 要有光
查看>>
WebGL自学教程——WebGL示例:13.0 代码整理
查看>>
WebGL自学教程——WebGL示例:14.0 代码整理
查看>>
恶心的社会
查看>>
中国式危机公关9加1策略(第五章 慎用信息控制策略)
查看>>
展现自己的人生智慧
查看>>
深入理解java多态性
查看>>
Java新手进阶:细说引用类型
查看>>