leetcode之Evaluate Reverse Polish Notation

原创 2015年11月18日 14:27:32

这道题是很基础的数据结构知识。唯一需要注意的是6/(-32)网站给的结果是0,python给的是-1,我也不知道为什么。。。代码如下:

class Solution(object):
    def evalRPN(self, tokens):
        """
        :type tokens: List[str]
        :rtype: int
        """
        if tokens == []:
            return 0
        if tokens == ['']:
            return 0
        listofnum = []
        symbol = ['+', '-', '*', '/']
        for i in tokens:
            if i in symbol:
                a = listofnum.pop()
                b = listofnum.pop()
                if i == '+':
                    listofnum.append(int(a) + int(b))
                if i == '-':
                    listofnum.append(int(b) - int(a))
                if i == '*':
                    listofnum.append(int(a) * int(b))
                if i == '/':
                    if int(b) < 0:
                        if int(a) > 0:
                            listofnum.append(-(-int(b) / int(a)))
                        else:
                            listofnum.append(int(b) / int(a))
                    else:
                        if int(a) < 0:
                            listofnum.append(-(int(b) / (-int(a))))
                        else:
                            listofnum.append(int(b) / int(a))
            else:
                listofnum.append(int(i))
            # print listofnum
        return int(listofnum[0])


LeetCode–Evaluate Reverse Polish Notation

LeetCode–Evaluate Reverse Polish Notation (LeetCode - 评价逆波兰表示法,即后缀表达式) By X Wang (LeetCode: 一个在线编...

[leetcode]Evaluate Reverse Polish Notation题解

描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

[C++]LeetCode: 98 Evaluate Reverse Polish Notation

题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators...

(leetcode)Evaluate Reverse Polish Notation

Evaluate Reverse Polish Notation 题目 Evaluate the value of an arithmetic expression in Reverse Pol...
  • fcunren
  • fcunren
  • 2014年08月18日 15:28
  • 282

leetcode笔记:Evaluate Reverse Polish Notation(逆波兰式的计算)

该题考查逆波兰式,也叫后缀表达式(将运算符写在操作数之后)。假设有一个表达式E,其后缀形式定义如下: 1. 如果E是一个变量或常量,则E的后缀式是E本身; 2. 如果E是E1 operator E...

LeetCode 150. Evaluate Reverse Polish Notation

LeetCode 150. Evaluate Reverse Polish Notation 栈

LeetCode|Evaluate Reverse Polish Notation

题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
  • hwb1992
  • hwb1992
  • 2014年04月20日 21:26
  • 615

LeetCode:《Evaluate Reverse Polish Notation》

#include #include #include #include using namespace std; class Solution { public: int evalRP...

leetcode 150. Evaluate Reverse Polish Notation

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

[LeetCode]Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode之Evaluate Reverse Polish Notation
举报原因:
原因补充:

(最多只允许输入30个字)