[LeetCode]题解(python):150-Evaluate Reverse Polish Notation
题目来源:
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的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
随机推荐
- Android中 Http请求
HttpClient public class MainActivity extends Activity { private Button button; @Override protected v ...
- spring的官方文真不错
引文不太好,但是通过别的版本的查看,发现spring的文档真心不错,内容详细明了. http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-fra ...
- panel控件 换行
Panel1.Controls.Add(new LiteralControl("<BR/>"));
- 【MVC】过滤器
APS.NET MVC中(以下简称“MVC”)的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理.这时候就用到了过滤器. MVC支持的过滤器 ...
- python中调用C++写的动态库
一.环境:Windows XP + Python3.2 1. dll对应的源文件(m.cpp): #include <stdio.h> extern "C" { _de ...
- Android 获取系统图库和相机照片 裁剪并显示
接上一篇 package com.example.image; import android.app.Activity; import android.content.Intent; import a ...
- Flex中如何通过horizontalTickAligned和verticalTickAligned样式指定线图LineChart横竖方向轴心标记的例子
原文http://blog.minidx.com/2008/12/03/1669.html 接下来的例子演示了Flex中如何通过horizontalTickAligned和verticalTickAl ...
- OpenLayers 添加OpenStreetMap(OSM)瓦片层示例
This article from:http://wiki.openstreetmap.org/wiki/OpenLayers_Simple_Example Deploy an OpenStreetM ...
- 多项式ADT的数组实现
/*删除表的正确方法*/ /*assume header*/ void DeleteList(List L) { Position p,Tmp; p=L->Next; while(p != NU ...
- 开发该选择Blocks还是Delegates(转)
原文链接:http://blog.stablekernel.com/blocks-or-delegates/ By Joe Conway,CEO | Jul 11,2013 11:29:00 AM 有 ...