题目来源:

  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的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  3. 【LeetCode】150. Evaluate Reverse Polish Notation

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

  4. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

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

  5. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

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

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

  10. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

随机推荐

  1. C++中socket编程

    原文:http://blog.csdn.net/cuiran/article/details/5854794 Server端 #include <WINSOCK2.H> #include ...

  2. 利用Oracle数据库的UTL_SMTP发送HTML 邮件

    Ok, that looks hard, but if you use this procedure I wrote, its really quite easy, it does all of th ...

  3. C# 获取本机IP地址以及转换字符串

    /// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址 ...

  4. Apache Tiles 2.x 应用指南(转)

    转自:http://jaymsimusic.iteye.com/blog/1138906 Apache Tiles 2.x 应用指南 博客分类: Apache Tiles   Jakarta Tile ...

  5. poj3292-类素数筛选法

    #include<iostream>using namespace std;const int N=1000002;int array[N]={0};int main(){ int n;  ...

  6. [Effective C++系列]-透彻了解inlining的里里外外

    Understand the ins and outs of inlining.   [原理] Inline函数背后的做法是将“对函数的每一个调用”都用函数本体(function body)替换之.其 ...

  7. 设计模式之UML类图

    在学设计模式的过程中经常碰到各式各样的UML类图.那些眼花缭乱的符号有什么含义呢? 类图含义 类图中的关系 从网上找来一张图作为实例 依赖关系:比如动物依赖氧气和水,这里如学生要依赖自行车.用虚线箭头 ...

  8. 转:HTML 5 控件事件属性

    Window 事件属性 window 对象触发的事件. 适用于 <body> 标签: 属性 值 描述 onafterprint script 在打印文档之后运行脚本 onbeforepri ...

  9. C++进程间通信(常用理解例子)-买票

    #include "stdafx.h" #include <iostream>using namespace std; #include "windows.h ...

  10. 以Python列表特性为切入点的求质数列表的方法

    一般,构造一个含有2-x之间所有质数的列表,我们采用最简单的遍历判断质数的方法: # 方法一 1 prime = [] def is_prime(n): if n <= 1: return Fa ...