博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-Evaluate Reverse Polish Notation (Python)
阅读量:4625 次
发布时间:2019-06-09

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

【问题】

Evaluate the value of an arithmetic expression in .

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

【代码】

class Solution:    # @param tokens, a list of string    # @return an integer    def evalRPN(self, tokens):        stack = []        for item in tokens:            if item not in ("+", "-", "*", "/"):                stack.append(int(item))            else:                op2 = stack.pop()                op1 = stack.pop()                if item == "+":                    stack.append(op1 + op2)                elif item == "-":                    stack.append(op1 - op2)                elif item == "*":                    stack.append(op1 * op2)                elif item == "/":                    stack.append(int(op1 *1.0 / op2))        return stack[0]

posted on
2017-07-22 15:14 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/mthoutai/p/7221424.html

你可能感兴趣的文章
题解 楼房重建
查看>>
Python汉字转换成拼音
查看>>
高德地图:定位、覆盖物
查看>>
抽象类不能实例化对象
查看>>
树状数组(hdu-4325,hdu-1166,pat-1057)
查看>>
C#引用类型参数,ref按引用传值
查看>>
Flume简介与使用(二)——Thrift Source采集数据
查看>>
原生对象-Array
查看>>
词法解析的基本原理
查看>>
IDEA安装
查看>>
MySQL分库分表
查看>>
PyQt5--TextDrag
查看>>
Netty轻量级对象池实现分析
查看>>
Eclipse中的Web项目自动部署到Tomcat
查看>>
web前端学习总结--HTML
查看>>
非主流测试洞见:系统思考
查看>>
上海买车流程
查看>>
ExtJs store操作
查看>>
不要使用Integer做HashMap的key,尤其在json序列化的时候
查看>>
操作符重载调用优先级
查看>>