150. Evaluate 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
class Solution:
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
for i in tokens:
if i not in ['+','-','*','/']:
stack.append(i)
else:
v2 = int(stack.pop())
v1 = int(stack.pop())
if i=='+':
stack.append(v1+v2)
if i=='-':
stack.append(v1-v2)
if i=='*':
stack.append(v1*v2)
if i=='/':
stack.append(v1/v2)
return int(stack[0])
150. Evaluate Reverse Polish Notation(逆波兰表达式)的更多相关文章
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- 150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如: ["2", "1&quo ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Leetcode150. Evaluate Reverse Polish Notation逆波兰表达式求值
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- Evaluate Reverse Polish Notation(逆波兰式)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java Evaluate Reverse Polish Notation(逆波兰式)
表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
随机推荐
- PowerShell----Automatic_Variables(预定义变量)
以下这些变量是由powershell创建和维护的.ls Variable: 可以获取到所有默认的变量, 每个版本的Powershell可能有差异 $$包含会话所收到的最后一行中的最后一个令牌. $? ...
- redhat6.4 zabbix3.0.2安装
zabbix不用说了,很好的服务器监控管理工具,还支持中文哈! 1.添加epel仓库,有更多可用的软件包 rpm -ivh http://download.fedoraproject.org/pub/ ...
- WPF InitializeComponent() 方法介绍
namespace WindowsApplication1 { /// <summary> /// Interaction logic for Window1.xaml /// </ ...
- 在scrollview中双击定点放大的代码
双击放大是 iPhone 的一个基本操作,第三方程序里引入这一功能的话,主要是在 scrollview 呈现一张图片或者 PDF 页面时,双击可以放大,主要代码如下 - (void)scrollVie ...
- Android Studio添加assets文件夹
Step #1:调出项目结构管理区域 View->Tool Windows->Project Step #2:结构管理区域选择“Project” Step #3:新建"asset ...
- Android 切换主题 (二)
Android 切换主题 (二) 背景 我原来写过一篇文章关于 android 切换主题的文章 -- Android 切换主题以及换肤的实现 , 里面介绍了如何使用 setTheme() 来切换主题, ...
- IIS中,当文件夹被删除时,防止应用程序重启的解决办法
如果你曾经修改了ASP.NET应用程序(dll文件),与修改了bin文件夹或Web.config文件(添加/删除/重命名的文件等),而该网 站在运行,你可能已经注意到,这将导致在AppDomain的重 ...
- 170418、vmware 安装 centos 开启网卡自启动
前言:CentOS虚拟机安装成功后,默认开机未启用网关,通过修改配置文件,启用网卡 开启方法如下: 1.登录系统,虚拟机安装完成后,第一次登录系统,系统只有一个账号,即超级管理员root账户,输入安装 ...
- UVALive 6933 Virus synthesis(回文树)
Viruses are usually bad for your health. How about ghting them with... other viruses? In this proble ...
- Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...